Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function wizard_page() {
- $steps = array(
- 'step1',
- 'step2',
- 'step3',
- );
- return drupal_get_form('multistep_form', $steps)
- . 'Multistep form';
- }
- //function fpn_customer_become_customer_submit($form, &$form_state) {
- //
- //}
- function multistep_form(&$form_state, $steps) {
- if (!$form_state['storage']['step']) {
- reset($steps);
- $form_state['storage']['steps'] = $steps;
- $form_state['storage']['step'] = $steps[key($steps)];
- $form_state['storage']['values'] = array();
- }
- $form = multistep_retrieve_step_form($form_state);
- settype($form['#submit'], 'array');
- $form['#submit'][] = 'multistep_process_submit';
- multistep_form_submit_buttons($form, $form_state);
- return $form;
- }
- function multistep_retrieve_step_form($form_state) {
- $step = $form_state['storage']['step'];
- $form = $step($form_state);
- if (!isset($form['#validate'])) {
- if (is_callable($step .'_validate'))
- $form['#validate'][] = $step .'_validate';
- }
- if (!isset($form['#submit'])) {
- if (is_callable($step .'_submit'))
- $form['#submit'][] = $step .'_submit';
- }
- dpm($form);
- return $form;
- }
- function multistep_process_submit($form, &$form_state) {
- $steps = $form_state['storage']['steps'];
- $step = $form_state['storage']['step'];
- reset($steps); $first_step = current($steps);
- end($steps); $last_step = current($steps);
- $form_state['storage']['values'][$step] = $form_state['values'];
- // dpm($form_state);
- if ($form_state['values']['op'] == $form['multistep_buttons']['next']['#value']) {
- if ($step == $last_step) {
- unset($form_state['storage']);
- }
- else {
- $index = array_search($step, $steps);
- $form_state['storage']['step'] = $steps[$index + 1];
- }
- }
- else if ($form_state['values']['op'] == $form['multistep_buttons']['back']['#value']) {
- $index = array_search($step, $steps);
- $form_state['storage']['step'] = $steps[$index - 1];
- }
- }
- function multistep_form_submit_buttons(&$form, $form_state) {
- $steps = $form_state['storage']['steps'];
- $step = $form_state['storage']['step'];
- reset($steps); $first_step = current($steps);
- end($steps); $last_step = current($steps);
- $buttons = array();
- if ($step != $first_step) {
- $buttons['back'] = array(
- '#type' => 'submit',
- '#value' => t('Back'),
- );
- }
- if ($step != $last_step) {
- $buttons['next'] = array(
- '#type' => 'submit',
- '#value' => t('Next'),
- );
- }
- else {
- $buttons['next'] = array(
- '#type' => 'submit',
- '#value' => t('Finish'),
- );
- }
- $form['multistep_buttons'] = $buttons;
- $form['multistep_buttons']['#tree'] = true;
- }
- function step1($form_state) {
- $form['input1'] = array(
- '#type' => 'textfield',
- '#title' => 'input1',
- );
- return $form;
- }
- function step1_submit($form_state) {
- dpm(__FUNCTION__);
- }
- function step1_validate($form_state) {
- dpm(__FUNCTION__);
- }
- function step2($form_state) {
- $form['input2'] = array(
- '#type' => 'textfield',
- '#title' => 'input2',
- );
- return $form;
- }
- function step2_submit($form_state) {
- dpm(__FUNCTION__);
- }
- function step2_validate($form_state) {
- dpm(__FUNCTION__);
- }
- function step3($form_state) {
- $form['input3'] = array(
- '#type' => 'textfield',
- '#title' => 'input3',
- );
- $form['#submit'][] = 'step3___submit';
- $form['#validate'][] = 'step3___validate';
- return $form;
- }
- function step3___submit($form_state) {
- dpm(__FUNCTION__);
- }
- function step3___validate($form_state) {
- dpm(__FUNCTION__);
- }
Add Comment
Please, Sign In to add comment