I've been working on a multi-step ajax form from the code in the examples module. I've got nested fieldsets that use #states to become visible based on a checkbox state. For some reason the values for those fieldsets are not being kept in ['storage']. The rest of the form values are being stored properly, but those are not for some reason.
function theraform_psysoctwo($form, &$form_state, $no_js_use = FALSE) {
// Provide a wrapper around the entire form, since we'll replace the whole
// thing with each submit.
$form['#prefix'] = '<div id="wizard-form-wrapper">';
$form['#suffix'] = '</div>';
$form['#tree'] = TRUE; // We want to deal with hierarchical form values.
$form['description'] = array(
'#markup' => '<div>' . t('Page One of Five')
. '</div>',
);
// $form_state['storage'] has no specific drupal meaning, but it is
// traditional to keep variables for multistep forms there.
$step = empty($form_state['storage']['step']) ? 1 : $form_state['storage']['step'];
$form_state['storage']['step'] = $step;
$format = 'm-d-Y';
switch ($step) {
case 1:
$form['step1'] = array(
'#type' => 'fieldset',
'#title' => t('Basic Patient Information'),
);
$form['step1']['first_name'] = array(
'#type' => 'textfield',
'#title' => t('First Name'),
'#default_value' => empty($form_state['values']['step1']['first_name']) ? '' : $form_state['values']['step1']['first_name'],
'#required' => TRUE,
'#size' => 25,
'#maxlength' => 25,
);
$form['step1']['middle_initial'] = array(
'#type' => 'textfield',
'#title' => t('M.I.'),
'#default_value' => empty($form_state['values']['step1']['middle_initial']) ? '' : $form_state['values']['step1']['middle_initial'],
'#size' => 1,
'#maxlength' => 3,
);
$form['step1']['last_name'] = array(
'#type' => 'textfield',
'#title' => t('Last Name'),
'#default_value' => empty($form_state['values']['step1']['last_name']) ? '' : $form_state['values']['step1']['last_name'],
'#size' => 1,
'#maxlength' => 3,
);
$form['step1']['admission_date'] = array(
'#type' => 'date_text',
'#title' => t('Admission Date'),
'#default_value' => empty($form_state['values']['step1']['admission_date']) ? '' : $form_state['values']['step1']['admission_date'],
'#date_format' => $format,
);
$form['step1']['substances'] = array(
'#type' => 'item',
'#title' => t('Please check all substances relevant to the patient:'),
);
$form['step1']['alcohol'] = array(
'#type' => 'checkbox',
'#title' => t('Alcohol'),
'#default_value' => empty($form_state['values']['step1']['alcohol']) ? '' : $form_state['values']['step1']['alcohol'],
);
$form['step1']['alc_info'] = array(
'#type' => 'fieldset',
'#title' => t('Alcohol Use'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#states' => array(
'visible' => array(
':input[name = "step1[alcohol]"]' => array('checked' => TRUE),
),
),
);
$form['step1']['alc_info']['alc_onset'] = array(
'#type' => 'textfield',
'#title' => t('Age of Onset'),
'#size' => 2,
'#maxlength' => 2,
'#default_value' => empty($form_state['values']['step1']['alc_onset']) ? '' : $form_state['values']['step1']['alc_onset'],
);
$form['step1']['alc_info']['alc_amount'] = array(
'#type' => 'textfield',
'#title' => t('Amount used and frequency of use'),
'#size' => 50,
'#maxlength' => 100,
'#default_value' => empty($form_state['values']['step1']['alc_amount']) ? '' : $form_state['values']['step1']['alc_amount'],
);
$form['step1']['alc_info']['alc_last_use'] = array(
'#type' => 'date_text',
'#title' => t('Date of last use'),
'#date_format' => $format,
'#default_value' => empty($form_state['values']['step1']['alc_last_use']) ? '' : $form_state['values']['step1']['alc_last_use'],
);
break;
case 2:
$form['step2'] = array(
'#type' => 'fieldset',
'#title' => t('Page 2'),
);
$form['step2']['treatment_history'] = array(
'#type' => 'select',
'#title' => t('Number of previous treatments patient has undergone'),
'#default_value' => empty($form_state['values']['step2']['treatment_history']) ? '' : $form_state['values']['step2']['treatment_history'],
'#options' => array(
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
7 => 8,
8 => 9,
9 => 10,
),
);
break;
case 3:
$form['step3'] = array(
'#type' => 'fieldset',
'#title' => t('Page 3'),
);
$form['step3']['city'] = array(
'#type' => 'textfield',
'#title' => t('Your city'),
'#default_value' => empty($form_state['values']['step3']['city']) ? '' : $form_state['values']['step3']['city'],
'#required' => TRUE,
);
break;
}
if ($step > 1) {
$form['prev'] = array(
'#type' => 'submit',
'#value' => t("Previous step"),
// Since all info will be discarded, don't validate on 'prev'.
'#limit_validation_errors' => array(),
// #submit is required to use #limit_validation_errors
'#submit' => array('ajax_example_wizard_submit'),
'#ajax' => array(
'wrapper' => 'wizard-form-wrapper',
'callback' => 'ajax_example_wizard_callback',
),
);
}
if ($step < 3) {
$form['next'] = array(
'#type' => 'submit',
'#value' => t('Next step'),
'#submit' => array('ajax_example_wizard_submit'),
'#ajax' => array(
'wrapper' => 'wizard-form-wrapper',
'callback' => 'ajax_example_wizard_callback',
),
);
}
if ($step == 3) {
$form['submit'] = array(
'#type' => 'submit',
'#value' => t("Submit your information"),
);
}
// This simply allows us to demonstrate no-javascript use without
// actually turning off javascript in the browser. Removing the #ajax
// element turns off AJAX behaviors on that element and as a result
// ajax.js doesn't get loaded.
// For demonstration only! You don't need this.
if ($no_js_use) {
// Remove the #ajax from the above, so ajax.js won't be loaded.
// For demonstration only.
unset($form['next']['#ajax']);
unset($form['prev']['#ajax']);
}
return $form;
}
function ajax_example_wizard_callback($form, $form_state) {
return $form;
}
/**
* Submit function for ajax_example_wizard.
*
* In AJAX this is only submitted when the final submit button is clicked,
* but in the non-javascript situation, it is submitted with every
* button click.
*/
function ajax_example_wizard_submit($form, &$form_state) {
// Save away the current information.
$current_step = 'step' . $form_state['storage']['step'];
if (!empty($form_state['values'][$current_step])) {
$form_state['storage']['values'][$current_step] = $form_state['values'][$current_step];
}
// Increment or decrement the step as needed. Recover values if they exist.
if ($form_state['triggering_element']['#value'] == t('Next step')) {
$form_state['storage']['step']++;
// If values have already been entered for this step, recover them from
// $form_state['storage'] to pre-populate them.
$step_name = 'step' . $form_state['storage']['step'];
if (!empty($form_state['storage']['values'][$step_name])) {
$form_state['values'][$step_name] = $form_state['storage']['values'][$step_name];
}
}
if ($form_state['triggering_element']['#value'] == t('Previous step')) {
$form_state['storage']['step']--;
// Recover our values from $form_state['storage'] to pre-populate them.
$step_name = 'step' . $form_state['storage']['step'];
$form_state['values'][$step_name] = $form_state['storage']['values'][$step_name];
}
// If they're done, submit.
if ($form_state['triggering_element']['#value'] == t('Submit your information')) {
$value_message = t('Your information has been submitted:') . ' ';
foreach ($form_state['storage']['values'] as $step => $values) {
$value_message .= "$step: ";
foreach ($values as $key => $value) {
$value_message .= "$key=$value, ";
}
}
drupal_set_message($value_message);
$form_state['rebuild'] = FALSE;
return;
}
// Otherwise, we still have work to do.
$form_state['rebuild'] = TRUE;
}
Comments
Comment #1
jtellis commentedComment #2
jtellis commentedComment #3
kenorb commented