Skip to content

Instantly share code, notes, and snippets.

@thsutton
Created November 1, 2010 04:37
Show Gist options
  • Save thsutton/657619 to your computer and use it in GitHub Desktop.
Save thsutton/657619 to your computer and use it in GitHub Desktop.
Multiple Step Forms
===================
This module implements a techniques that can make your Drupal forms easier to
implement and easier to use: [breaking a form into multiple steps][1].
[1]: http://www.appnovation.com/create-multiple-step-form-drupal-6
core = 6.x
name = Test form
description = A multi-page form for testing purposes.
version = 0.0
package = Forms
dependencies[] = devel
<?php
/** @file
*
* A simple multi-page form to test FAPI modules.
*
* @author Thomas Sutton <me@thomas-sutton.id.au>
* @copyright Bouncing Orange
* @created 2010-11-01
*/
/**
* Implementation of hook_menu().
*/
function multistepform_menu() {
return array(
'example/form' => array(
'title' => t('Example form'),
'page callback' => 'drupal_get_form',
'page arguments' => array('multistepform_multistepform'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
),
);
}
/**
* A multi-page FAPI form.
*
* This function implements a multiple-step FAPI form. It does so using
*/
function multistepform_multistepform(&$form_state) {
//
// Determine which page of the form to display.
//
if (!isset($form_state['storage']['page'])) {
$form_state['storage']['page'] = 1;
}
$page = $form_state['storage']['page'];
//
// Build the form for the current page.
//
$skipping = $form_state['post']['op'] == 'Back';
if ($skipping) {
$form_state['rebuild'] = TRUE;
}
$form = array();
switch ($page) {
case 1:
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#required' => !$skipping,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Next'),
);
break;
case 2:
$form['face'] = array(
'#type' => 'textarea',
'#title' => t("Please describe your face"),
'#required' => !$skipping,
);
$form['back'] = array(
'#type' => 'submit',
'#value' => t('Back'),
'#submit' => array('multistepform_multistepform_back'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
break;
}
$form['#afterbuild'][] = 'multistepform_back_validation';
return $form;
}
function multistepform_form_alter(&$form, &$form_state, $form_id) {
dsm(array("from" => "form_alter", "form_id" => $form_id, "form" => $form, "form_state" => $form_state));
$form_state['rebuild'] = TRUE;
if ('Next' == $form_state['post']['op']) {
$form['face']['#required'] = FALSE;
}
}
function multistepform_back_validation(&$form, &$form_state) {
}
/**
* Validate the form defined above.
*/
function multistepform_multistepform_validate(&$form, &$form_state) {
}
/**
* Process each page submission and then the entire form.
*
* Each page submission is saved into the $form_state['storage'] array (which
* the magic of FAPI maintains for us) until the whole lot is processed when
* the last page is submitted.
*/
function multistepform_multistepform_submit($form, &$form_state) {
//
// Determine which page of the form to process.
//
$page = $form_state['storage']['page'];
//
// Save the values for processing.
//
$form_state['rebuild'] = TRUE;
$form_state['storage'][$page]['values'] = $form_state['values'];
$form_state['storage']['page']++;
//
// If the last page was submitted, process the whole lot.
//
if (2 == $page) {
// Process form with data from $form_state['storage'][$page]['values']
$values = array();
foreach ($form_state['storage'] as $k => $v) {
if (is_numeric($k)) {
$values = array_merge($values, $v['values']);
}
}
}
}
function multistepform_multistepform_back($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['storage']['page']--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment