3

I created a multistep node form in Drupal7 using the multistep module (http://drupal.org/project/multistep)

I patched the module so it would work (previously Drupal complained about required fields that weren't on the current step). The module is working fine now, the only thing is that if I press the previous button, it still validates the current step. I want to skip this validation, so the current step is only validated if I press the next or done button.

I digged into the module code, but really cannot figure out what to search for.

Can anyone help me on this?

Edit: Here's my form_alter function for multistep:

function multistep_form_alter(&$form, &$form_state) {
if (isset($form['#multistep'])) {
  $excluded_children = _multistep_gather_excluded_fieldgroup_children($form, $form['#multistep']['current']);
  $current_children = _multistep_gather_current_children($form, $excluded_children);

  $prev_excluded_children = _multistep_gather_excluded_fieldgroup_children($form, $form['#multistep']['previous']);
  $prev_current_children = _multistep_gather_current_children($form, $prev_excluded_children);

  // Apply limit validations to all submit fields.
  foreach (element_children($form['actions']) as $key) {
    if ($key == 'previous') {
        if (isset($form['actions'][$key]['#type']) && $form['actions'][$key]['#type'] == 'submit') {
          $form['actions'][$key]['#limit_validation_errors'] = $prev_current_children;      
        }
    }
    else {
      if (isset($form['actions'][$key]['#type']) && $form['actions'][$key]['#type'] == 'submit') {
          $form['actions'][$key]['#limit_validation_errors'] = $current_children;       
      }
    }   
  }
}

}

  • Ok, I managed to save current step values in _SESSION and restore them later. Now the only problem is that field validation switches off on restored steps. I just replaced $form['field_something'] with the previously saved data. The field gets populated, but then I can go forth without validating required fields. – Moha Oct 14 '12 at 14:35
  • Hard to tell what's wrong without seeing the code, but some things you could check: 1) make sure you aren't getting rid of important field data when replacing - for example if you save input data into the session you probably just need to add it back as '#default_value' later on. 2) make sure your usage of '#limit_validation_errors' isn't freeing those fields from validation. – Madis Oct 14 '12 at 19:06
  • I was probably overwriting some important objects as I simply replaced the whole field_something array with the saved data. I will now try to iterate through the array and store only the values, then populate the form with them. – Moha Oct 15 '12 at 8:15
3

I'm not familiar with the multistep module, but in Drupal 7 this functionality can normally be accomplished by using #limit_validation_errors. It would probably be best to alter the form(s) that you need with hook_form_alter (or a variation of it) in a custom module. A basic untested example of what I described:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'THE_RELEVANT_FORM_ID') {
    $form['THE_PREVIOUS_BUTTON_KEY_NAME']['#limit_validation_errors'] = array();
  }
}
  • Thanks, I think the solution will be something this way. I tried to implement this into the module, but right now I get an EntityMalformedException – Moha Oct 13 '12 at 19:54
  • YES! Thank you for your hint. Now I am able to go back, but there's one more thing: when pushing the back button, the contents are not saved. I'd like to save those fields upon hitting the back button, even if there's some unfilled required fields. Do you think it's possible? I'll post my form_alter in the original question. – Moha Oct 13 '12 at 20:18
  • Normally you either validate + save or don't validate and don't save. If you need some inbetween solution then you could add custom validation/submit handlers to the form where you can work with the sent information. If you save data into the database it should always be validated though otherwise you may run into other problems. – Madis Oct 14 '12 at 18:54
  • Thanks. I managed to save user input values upon hitting back button and restoring them later. Your answer helped me a lot. – Moha Oct 17 '12 at 6:18
  • @Moha Could you please post the final code? – Sandesh Yadav Jan 13 '17 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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