1 form_example_tutorial.inc | form_example_tutorial_6_validate($form, &$form_state) |
Validation handler for Tutorial 6.
Now we add 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'].
Notice the name of the function. It is simply the name of the form followed by '_validate'. This is always the name of the default validation function. An alternate list of validation functions could have been provided in $form['#validate'].
See also
File
- modules/
examples/ form_example/ form_example_tutorial.inc, line 259 - This is the form API tutorial.
Code
function form_example_tutorial_6_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', t('Enter a year between 1900 and 2000.'));
}
}