You are here

function form_example_wizard in Examples for Developers 7

The primary formbuilder function for the wizard form.

This is the form that you should call with drupal_get_form() from your code, and it will include the rest of the step forms defined. You are not required to change this function, as this will handle all the step actions for you.

This form has two defined submit handlers to process the different steps:

  • Previous: handles the way to get back one step in the wizard.
  • Next: handles each step form submission,

The third handler, the finish button handler, is the default form_submit handler used to process the information.

You are not required to change the next or previous handlers, but you must change the form_example_wizard_submit handler to perform the operations you need on the collected information.

Related topics

1 string reference to 'form_example_wizard'
form_example_menu in form_example/form_example.module
Implements hook_menu().

File

form_example/form_example_wizard.inc, line 81
Extensible wizard form example.

Code

function form_example_wizard($form, &$form_state) {

  // Initialize a description of the steps for the wizard.
  if (empty($form_state['step'])) {
    $form_state['step'] = 1;

    // This array contains the function to be called at each step to get the
    // relevant form elements. It will also store state information for each
    // step.
    $form_state['step_information'] = _form_example_steps();
  }
  $step =& $form_state['step'];
  drupal_set_title(t('Extensible Wizard: Step @step', array(
    '@step' => $step,
  )));

  // Call the function named in $form_state['step_information'] to get the
  // form elements to display for this step.
  $form = $form_state['step_information'][$step]['form']($form, $form_state);

  // Show the 'previous' button if appropriate. Note that #submit is set to
  // a special submit handler, and that we use #limit_validation_errors to
  // skip all complaints about validation when using the back button. The
  // values entered will be discarded, but they will not be validated, which
  // would be annoying in a "back" button.
  if ($step > 1) {
    $form['prev'] = array(
      '#type' => 'submit',
      '#value' => t('Previous'),
      '#name' => 'prev',
      '#submit' => array(
        'form_example_wizard_previous_submit',
      ),
      '#limit_validation_errors' => array(),
    );
  }

  // Show the Next button only if there are more steps defined.
  if ($step < count($form_state['step_information'])) {

    // The Next button should be included on every step.
    $form['next'] = array(
      '#type' => 'submit',
      '#value' => t('Next'),
      '#name' => 'next',
      '#submit' => array(
        'form_example_wizard_next_submit',
      ),
    );
  }
  else {

    // Just in case there are no more steps, we use the default submit handler
    // of the form wizard. Call this button Finish, Submit, or whatever you
    // want to show. When this button is clicked, the
    // form_example_wizard_submit handler will be called.
    $form['finish'] = array(
      '#type' => 'submit',
      '#value' => t('Finish'),
    );
  }

  // Include each validation function defined for the different steps.
  if (function_exists($form_state['step_information'][$step]['form'] . '_validate')) {
    $form['next']['#validate'] = array(
      $form_state['step_information'][$step]['form'] . '_validate',
    );
  }
  return $form;
}