form_example_tutorial_8

6 form_example_tutorial.inc form_example_tutorial_8(&$form_state)
7 form_example_tutorial.inc form_example_tutorial_8($form, &$form_state)
8 form_example_tutorial.inc form_example_tutorial_8($form, &$form_state)

Example 8: A simple multistep form with a Next and a Back button.

Handbook page: http://drupal.org/node/717750.

For more extensive multistep forms, see form_example_wizard.inc

Adds logic to our form builder to give it two pages. The AJAX Example's Wizard Example gives an AJAX version of this same idea.

See also

form_example_tutorial_8_submit()

form_example_tutorial_8_validate()

form_example_tutorial_8_page_two()

form_example_tutorial_8_page_two_submit()

form_example_tutorial_8_next_submit()

form_example_tutorial.inc

Related topics

1 string reference to 'form_example_tutorial_8'

File

form_example/form_example_tutorial.inc, line 393
This is the Form API Tutorial from the handbook.

Code

function form_example_tutorial_8($form, &$form_state) {

  // Display page 2 if $form_state['page_num'] == 2
  if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
    return form_example_tutorial_8_page_two($form, $form_state);
  }

  // Otherwise we build page 1.
  $form_state['page_num'] = 1;

  $form['description'] = array(
    '#type' => 'item', 
    '#title' => t('A basic multistep form (page 1)'),
  );

  $form['first'] = array(
    '#type' => 'textfield', 
    '#title' => t('First name'), 
    '#description' => "Please enter your first name.", 
    '#size' => 20, 
    '#maxlength' => 20, 
    '#required' => TRUE, 
    '#default_value' => !empty($form_state['values']['first']) ? $form_state['values']['first'] : '',
  );
  $form['last'] = array(
    '#type' => 'textfield', 
    '#title' => t('Last name'), 
    '#default_value' => !empty($form_state['values']['last']) ? $form_state['values']['last'] : '',
  );
  $form['year_of_birth'] = array(
    '#type' => 'textfield', 
    '#title' => "Year of birth", 
    '#description' => 'Format is "YYYY"', 
    '#default_value' => !empty($form_state['values']['year_of_birth']) ? $form_state['values']['year_of_birth'] : '',
  );
  $form['next'] = array(
    '#type' => 'submit', 
    '#value' => 'Next >>', 
    '#submit' => array('form_example_tutorial_8_next_submit'), 
    '#validate' => array('form_example_tutorial_8_next_validate'),
  );
  return $form;
}

Comments

Additional Form Validate Function

This code example also shows how to specify a form validate function other than the default. The array that is created for the NEXT button uses the #validate key to specify a validate handler specifically for when the user presses NEXT, instead of the default handler that should be executed when the SUBMIT button is pressed on page 2 of the 2-page form.

Login or register to post comments