I'm working on a multistep form using the example given in Pro Drupal Development as a base. That is, I'm using a step counter stored in $form_state['storage']['step'] and checking its value at the top of the form builder to determine what elements to display.

I've set up a back button and I check for it's value and determine if I need to increment or decrement the step counter. What I'm running into is that it still validates the current step and required fields cause it to fail.

I've tried moving the step counter check and decrement from the submit function to the form build function (ala http://drupal.org/node/428366) and that displays the correct page (the one it moved back to) but also shows the errors raised from validating the first step. For instance, if you click back on step 2 and there are validation errors, step 1 is displayed with the errors from step 2's validation failure.

Anyone have a better way to do this? Either I need a means to shortcircuit validation entirely or I need a way to clear errors before they are displayed.

Many thanks...

Comments

barrett’s picture

bump

barrett’s picture

I found a solution that works.

At the top of my validation function, I put

if($form_state['values']['op']==t('Back')) {
  drupal_get_messages('error');
  return TRUE;
}

That unsets the messages and errors created by built in validation (e.g., empty required fields) and breaks out of the function before any of my custom validation happens.

onelittleant’s picture

This does not clear the form error state if, for example, you have empty required fields. It eliminates the messages, but does not unset the validation errors on the fields. To get this working, I had to do something more like this:

if($form_state['values']['op']==t('Back')) {
  // Clear the error messages.
  drupal_get_messages('error');
  // Clear the form error state.
  form_set_error(null, '', true);
  return TRUE;
}
saemie’s picture

In Drupal 7 just replace form_set_error(null, '', true); with form_clear_error().

Hopefully this will help someone.

jaypan’s picture

Here is a tutorial that explains how to do multi-step AJAX forms, with a back-button that does not validate the submitted data: http://www.jaypan.com/tutorial/drupal-7-ajax-driven-multi-step-forms-non...

(Note: Shameless plug of tutorial I wrote)

Contact me to contract me for D7 -> D10/11 migrations.