Question

I'm building a multistep form and I don't want users to lose any storaged information if they accidentally refresh the page. Steps to reproduce:

1) Press 'ok'-button
2) var_dump($form_state['multistep']['step']) == 2
3) Refresh page
4) var_dump($form_state['multistep']['step']) still == 2
5) Press 'ok'-button
6) var_dump($form_state['multistep']['step']) == 3
7) Refresh page
8) var_dump($form_state['multistep']['step']) == 2 rather than 3

There must be some Drupal form mechanic issue here that i'm not aware of. Can anyone explain why this happens? Example code below.

function multistep_test_form($form, &$form_state) {
    if( !isset($form_state['multistep']['step']) ) {
        $form_state['multistep']['step'] = 1;
    } else {
        var_dump($form_state['multistep']['step']);
    }

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'ok'
    );

    return $form;
}

function multistep_test_form_submit($form, &$form_state) {
    $form_state['rebuild'] = TRUE;
    $form_state['multistep']['step']++;
}
Was it helpful?

Solution

Values are stored in $form_state['page_values'].

$form_state['page_values'][1] = $form_state['values'];

Check the example module in the API docs for more detailed example.

Example Form

Example Form submit handler to carry values

Validate values (if needed)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top