Wednesday, August 22, 2012

What is the difference between $form & $form_state in Drupal

I am working with drupal 7 really its crazy but some how i confused  in drupal $form & $form_state

I discussed with many people now i got some clarification on this $form & $form_state values.$form defines the form, $form_state carries information about the processed form.

How to validate froms in Drupal 7?

Drupal Form validations

Here you can get the form values from form_state 

Below is the simple example for form validations  

function test_form_validate($form, &$form_state) {
  if (
$form_state['values']['name'] == '') {
    
form_set_error('name', t('You must select a name for this group of settings.'));
  }
}

Drupal 7 Basic form with submit handler

Bellow code tells about the basic form submit in drupal7

<?php
function my_module_menu() {
 
$items = array();
 
$items['my_module/form'] = array(
   
'title' => t('My form'),
   
'page callback' => 'my_module_form',
   
'access arguments' => array('access content'),
   
'description' => t('My form'),
   
'type' => MENU_CALLBACK,
  );
  return
$items;

}

function my_module_form() {
  return
drupal_get_form('my_module_my_form');
}


/*
  # Sample registration form 
*/
function my_module_my_form($form_state) {
 
$form['name'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('Name'),
   
'#collapsible' => TRUE,
   
'#collapsed' => FALSE,
  );
 
$form['name']['first'] = array(
   
'#type' => 'textfield',
   
'#title' => t('First name'),
   
'#required' => TRUE,
   
'#default_value' => "First name",
   
'#description' => "Please enter your first name.",
   
'#size' => 20,
   
'#maxlength' => 20,
  );
 
$form['name']['last'] = array(
   
'#type' => 'textfield',
   
'#title' => t('Last name'),
   
'#required' => TRUE,
  );
 
$form['year_of_birth'] = array(
   
'#type' => 'textfield',
   
'#title' => "Year of birth",
   
'#description' => 'Format is "YYYY"',
  ); 
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => 'Submit',
  );
  return
$form;
}


function my_module_my_form_validate($form, &$form_state) {
   
$year_of_birth = $form_state['values']['year_of_birth'];
    if (
$year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
      

  form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
    }
}


 function my_module_my_form_submit($form, &$form_state) {
   
drupal_set_message(t('The form has been submitted.'));
}


?>

Wednesday, August 15, 2012

Drupal 7 Confirm Password form alter example

Description: Format a pair of password fields, which do not validate unless the two entered passwords match.
 
<?php
$form
['pass'] = array(
  '#type'
=> 'password_confirm',
  '#title'
=> t('Password'),
  '#size'
=> 25,
);
?>

How to create checkboxes in Drupal form alter?

Description: Format a set of checkboxes. #options is an associative array, where the key is the #return_value of the checkbox and the value is displayed. The #options array cannot have a 0 key, as it would not be possible to discern checked and unchecked states.
 
<?php
$form
['high_school']['tests_taken'] = array(
  
'#type' => 'checkboxes',
  
'#options' => drupal_map_assoc(array(t('SAT'), t('ACT'))),
  
'#title' => t('What standardized tests did you take?'),
...
),
?>