Examples demonstrating the Drupal Form API.

The Form Example module is a part of the Examples for Developers Project and provides various Drupal Form API Examples. You can download and experiment with this code at the Examples for Developers project page.

Parent topics


form_example/form_example.module, line 8
Examples demonstrating the Drupal Form API.


Namesort descending Location Description
form_example_element_info form_example/form_example.module Implements hook_element_info().
form_example_help form_example/form_example.module Implements hook_help().
form_example_intro form_example/form_example.module Page callback for our general info page.
form_example_menu form_example/form_example.module Implements hook_menu().
form_example_states_form form_example/form_example_states.inc States demo form.
form_example_theme form_example/form_example.module Implements hook_theme().
form_example_tutorial form_example/form_example_tutorial.inc Main Form tutorial page.
form_example_tutorial_1 form_example/form_example_tutorial.inc Tutorial Example 1.
form_example_tutorial_10 form_example/form_example_tutorial.inc Example 10: A form with a file upload field.
form_example_tutorial_10_submit form_example/form_example_tutorial.inc Submit handler for form_example_tutorial_10().
form_example_tutorial_10_validate form_example/form_example_tutorial.inc Validate handler for form_example_tutorial_10().
form_example_tutorial_11 form_example/form_example_tutorial.inc Example 11: adding a confirmation form.
form_example_tutorial_11_confirm_name form_example/form_example_tutorial.inc Example 11: A form generated with confirm_form().
form_example_tutorial_11_confirm_name_submit form_example/form_example_tutorial.inc Submit function for form_example_tutorial_11_confirm_form().
form_example_tutorial_11_submit form_example/form_example_tutorial.inc Submit function for form_example_tutorial_11().
form_example_tutorial_2 form_example/form_example_tutorial.inc This is Example 2, a basic form with a submit button.
form_example_tutorial_3 form_example/form_example_tutorial.inc Example 3: A basic form with fieldsets.
form_example_tutorial_4 form_example/form_example_tutorial.inc Example 4: Basic form with required fields.
form_example_tutorial_5 form_example/form_example_tutorial.inc Example 5: Basic form with additional element attributes.
form_example_tutorial_6 form_example/form_example_tutorial.inc Example 6: A basic form with a validate handler.
form_example_tutorial_6_validate form_example/form_example_tutorial.inc Validation handler for Tutorial 6.
form_example_tutorial_7 form_example/form_example_tutorial.inc Example 7: With a submit handler.
form_example_tutorial_7_submit form_example/form_example_tutorial.inc Submit function for form_example_tutorial_7().
form_example_tutorial_7_validate form_example/form_example_tutorial.inc Validation function for form_example_tutorial_7().
form_example_tutorial_8 form_example/form_example_tutorial.inc Example 8: A simple multistep form with a Next and a Back button.
form_example_tutorial_8_next_submit form_example/form_example_tutorial.inc Submit handler for form_example_tutorial_8() next button.
form_example_tutorial_8_next_validate form_example/form_example_tutorial.inc Validate handler for the next button on first page.
form_example_tutorial_8_page_two form_example/form_example_tutorial.inc Returns the form for the second page of form_example_tutorial_8().
form_example_tutorial_8_page_two_back form_example/form_example_tutorial.inc Back button handler submit handler.
form_example_tutorial_8_page_two_submit form_example/form_example_tutorial.inc The page 2 submit handler.
form_example_tutorial_9 form_example/form_example_tutorial.inc Example 9: A form with a dynamically added new fields.
form_example_tutorial_9_add_name form_example/form_example_tutorial.inc Submit handler for "Add another name" button on form_example_tutorial_9().
form_example_tutorial_9_remove_name form_example/form_example_tutorial.inc Submit handler for "Remove name" button on form_example_tutorial_9().
form_example_tutorial_9_submit form_example/form_example_tutorial.inc Submit function for form_example_tutorial_9().
form_example_tutorial_9_validate form_example/form_example_tutorial.inc Validate function for form_example_tutorial_9().
form_example_wizard form_example/form_example_wizard.inc The primary formbuilder function for the wizard form.
form_example_wizard_location_info form_example/form_example_wizard.inc Returns form elements for the 'location info' page of the wizard.
form_example_wizard_location_info_validate form_example/form_example_wizard.inc Custom validation form for the 'location info' page of the wizard.
form_example_wizard_next_submit form_example/form_example_wizard.inc Submit handler for the 'next' button.
form_example_wizard_other_info form_example/form_example_wizard.inc Returns form elements for the 'other info' page of the wizard.
form_example_wizard_personal_info form_example/form_example_wizard.inc Returns form elements for the 'personal info' page of the wizard.
form_example_wizard_previous_submit form_example/form_example_wizard.inc Submit handler for the "previous" button.
form_example_wizard_submit form_example/form_example_wizard.inc Wizard form submit handler.
_form_example_steps form_example/form_example_wizard.inc Returns the list of steps and their associated forms.


Namesort descending Location Description
FormExampleTestCase form_example/form_example.test Default test case for the form_example module.


thomas_rendleman Go to Django for a better experience.’s picture

//////////////// Tutorial Example 6 //////////////////////

* Example 6: A basic form with a validate handler.
* From http://drupal.org/node/717736
* @see form_example_tutorial_6_validate()
function form_example_tutorial_6($form, &$form_state) {
$form['description'] = array(
'#type' => 'item',
'#title' => t('A form with a validation handler'),

$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;

thomas_rendleman Go to Django for a better experience.’s picture

* 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'] (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 always the name of the default validation
* function. An alternate list of validation functions could have been provided
* in $form['#validate'].
* @see form_example_tutorial_6()
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.'));