Thursday, September 29, 2011

Clearing Form values in Drupal

function my_module_my_form($form_state) {
 
$form['name'] = array(
   
'#type' => 'fieldset',
   
'#title' => t('Name'),
   
'#collapsible' => TRUE,
   
'#collapsed' => FALSE,
  );
 
 
// Removes the #required property and
  // uses the validation function instead.
 
$form['name']['first'] = array(
   
'#type' => 'textfield',
   
'#title' => t('First name'),
   
'#default_value' => "First name",
   
'#description' => "Please enter your first name.",
   
'#size' => 20,
   
'#maxlength' => 20,
  );
 
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => 'Submit',
  );


 
// Adds a new button to clear the form. The #validate property
  // directs the form to use a new validation handler function in place
  // of the default.
 
$form['clear'] = array(
   
'#type' => 'submit',
   
'#value' => 'Reset form',
   
'#validate' => array('my_module_my_form_clear'),
  );
 
  return
$form;
}


// This is the new validation handler for our Reset button. Setting
// the $form_state['rebuild'] value to TRUE, clears the form and also
// skips the submit handler.
function my_module_my_form_clear($form, &$form_state) {
   
$form_state['rebuild'] = TRUE;
}

No comments:

Post a Comment

Followers