1

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;
}

function _step_2($form, &$form_state) {
    ....
    $form['Name'] = array(
        '#title' => t('Name'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#maxlength' => 80,
        '#size' => 80,
    );

        $form['Street'] = array(
        '#title' => t('Street'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#maxlength' => 80,
        '#size' => 80,
    );

    ......

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

    $form['add'] = array(
            '#type' => 'submit',
            '#value' => 'Collega toevoegen',
        );
}

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?

2
  • can you pls show at least one field in the step 2 ? Most supposedly you are filling the values in there. And pls have a look at the examples module : drupal.org/project/examples , a similar usecase is covered there :)
    – mojzis
    Jun 11, 2012 at 13:41
  • Thanks for you help. I have taken a look at the examples. The different steps are working fine but the problem exists when step 2 (name, adres, etc.) is asked. And it should be possible to add another person to the list in this step, so that's why I've added another button 'add another' besides 'next'. P.S. orginal post for _step2 form
    – kim.kaho
    Jun 11, 2012 at 14:38

1 Answer 1

0

When you want to add a new set of values, you have to build the form in a different way - the fields that should be "multiplicated" have to be in an array.

so, your

$form['Name'] = array(
        '#title' => t('Name'),
        '#type' => 'textfield',

should instead look like this :

$form['step2'][$i]['Name'] = array( ...

where $i is set to zero in the begining and increased by the callback for your "add another" button. The "step2" fieldset is there just to help you distinguish the parts that are repeated from the others - its not necessary i guess. The aformentioned examples module has nice examples how to acheive that even with ajax, so you dont have to reload the whole thing. Dont forget to set

$form['#tree'] = TRUE;

so that you can process the several sets of results.

2
  • Thanks for your input. I will give this and the ajax methods a try.
    – kim.kaho
    Jun 12, 2012 at 7:18
  • thanks for your answer. Everything worked out fine. I've used the AJAX method inf the final result.
    – kim.kaho
    Jun 15, 2012 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.