Community Documentation

6. Basic form with validate handler

Last updated February 18, 2010. Created by bekasu on February 18, 2010.
Log in to edit this page.

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.

<?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');
}

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,
  );
 
 
// 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&#58;//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

in Drupal7, the page callback

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

Both solutions are valid

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

works both ways

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.

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here