1
votes

I'm creating a multistep form in Drupal 7 - FAPI.

In the main hook_form() the redirection to the different steps are handeled.

function hook_form($form, &$form_state) { 
    if (empty($form_state['storage']['step'])) {
        $form_state['storage']['step'] = 0;
    }  

    switch($form_state['storage']['step']) {
        case 0:
          $form=  _step_0($form, $form_state);
        break;
        case 1:
          $form =  _step_1($form, $form_state);
        break;
        case 2:
          $form =  _step_2($form, $form_state);
        break;
        case 3:
          $form =  _step_3($form, $form_state);
        break;
        case 99:
          $form =  _step_end($form, $form_state);
        break;
    }
    return $form;
}

In step 2 I have an optional button 'Add onther' which saves the form_values into storage and reloads the same form (step 2). But when it reloads this form for the seconds time. the previous form values are being represented as default form values and not a blank (new) form, which is wanted.

function inschrijven_form_submit($form, &$form_state) {
    switch ($form_state['storage']['step']) {

        case 0:
          switch($form_state['values']['op']) {
            case 'Next' :
              ....
              $form_state['storage']['step'] = 1;
            break;
          }
        break;

        case 1:
          switch($form_state['values']['op']) {
            case 'Next' :
              ....
              $form_state['storage']['step'] = 2;
            break;
          }
        break;

        case 2:
            switch($form_state['values']['op']) {
                case 'Next' :
                    ....
                    $form_state['storage']['step'] = 3;
                break;

                case 'Add another' :
                    $form_state['rebuild'] = TRUE;
                    ....
                    $form_state['storage']['step'] = 2;
                    return;
                break;

        .....

    }
}

I've try to clear the form_state['values'] manually after each submit but with no success. Who can this multistep with add another functionality be achieved?

1
I tried your solution posted on drupal.stackexchange.com/questions/33740/… which gave me the wanted result.kim.kaho

1 Answers

0
votes

The function that you are looking for is form_set_value($element, $value, &$form_state).

The forms api is a bit tricky. I have reasoned while working with the FAPI that $form_state always reflects the $form variable that is passed into the form. $form_state just makes it easy to get to the values. Any time you switch to another validation, it seems that $form_state gets updated from the values of '$form'.

If you use the form_set_value you should be able to pass the state between validations.

Cheers! http://api.drupal.org/api/drupal/includes!form.inc/function/form_set_value/7