Code sample #6:

This adds a new form field and a way to validate it with a validate function, also referred to as a validate handler.


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

function my_module_my_form($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,
  );
  
  // New form field added to permit entry of year of birth.
  // The data entered into this field will be validated with
  // the default validation function.
  $form['year_of_birth'] = array(
    '#type' => 'textfield',
    '#title' => "Year of birth",
    '#description' => 'Format is "YYYY"',
  ); 
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

// This adds a handler/function to validate the data entered into the 
// "year of birth" field to make sure it's between the values of 1900 
// and 2000. If not, it displays an error. The value report is // $form_state['values'] (see http://drupal.org/node/144132#form-state).
//
// Notice the name of the function. It is simply the name of the form 
// followed by '_validate'. This is the default validation function.
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.');
  }
}

Comments

alexmoreno’s picture

in Drupal7, the page callback is done calling directly the my_module_my_form function

function myform_menu() {
  $items = array();

$items['my_module/form'] = array(
    'title' => t('My form'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_my_form'),
    'access callback' => TRUE,
    'description' => 'Tutorial 7: Form with a submit handler',
    'type' => MENU_LOCAL_TASK,
	
);

  return $items;
}

otherwise, fields like

$form_state['values']['year_of_birth'];

will be empty when called from my_module_my_form_validate

francesco.giuffrida’s picture

I tested on drupal 7 and both the options are OK
you can call 'my_module_my_form', or pass through 'my_module_form'

kristapss’s picture

well i guess it should work exactly the same! because you are calling the same drupal_get_form() and in the example its just done through a local function.