I have a form that displays two submit buttons. The first submit button sets $form_state['storage'] to a value. The second submit button then reads this $form_state['storage'] value. If the value is set, then a success message is displayed. If the value is not set, then a fail message is displayed.
Here is the code that will reproduce my issue:
function mymodule_test_admin() {
return drupal_get_form('mymodule_test_form');
}
function mymodule_test_form(&$form_state) {
$form['mymodule_test_form1'] = array(
'#type' => 'fieldset',
'#title' => t('test 1'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE
);
$form['mymodule_test_form1']['submit'] = array(
'#type' => 'submit',
'#value' => t('button 1'),
'#submit' => array('mymodule_test_form1_submit')
);
$form['mymodule_test_form2'] = array(
'#type' => 'fieldset',
'#title' => t('test 2'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#tree' => TRUE
);
$form['mymodule_test_form2']['submit'] = array(
'#type' => 'submit',
'#value' => t('button 2'),
'#submit' => array('mymodule_test_form2_submit')
);
return $form;
}
function mymodule_test_form1_submit($form, &$form_state) {
$form_state['storage']['test_1'] = 'test 1';
drupal_set_message(t('@message', array('@message' => $form_state['storage']['test_1'])));
}
function mymodule_test_form2_submit($form, &$form_state) {
if (isset($form_state['storage']['test_1'])) {
drupal_set_message(t('success'));
} else {
drupal_set_message(t('fail!'));
}
}
When you click the first submit button, $form_state['storage'] is properly set. When you click the second submit button, the message "success" is displayed. So far so good. Now do a page refresh. The message "fail!" is displayed.
So everything works right up until the page refresh. The page refresh essentially is only calling the second submit function. In theory, $form_state['storage'] should still be populated and the message displayed should be "success". However, taking a look at the $form_state dump shows $form_state['storage'] is NULL after the page refresh. I can't tell if my code logic is wrong or if $form_state['storage'] is being cleared on the page refresh.
Any ideas?
Thanks for your help.
You will need to rebuild form at the end of processing form1_submit, this will keep the previously submitted values intact. This is some what a scenario of multistep forms, but a bit different from the way it was done in Drupal 5.
function mymoduel_test_form1_submit($form, &$form_state) {
$form_state['storage']['test_1'] = 'test 1';
drupal_set_message(t('@message', array('@message' => $form_state['storage']['test_1'])));
$form_state["rebuild"] = TRUE;
}
Hope this helps, Sarfaraz