Skip to main content

How to remove validation errors with a back button on a Drupal multistep form?

If you create a form in Ajax and multistep, you may encounter a problem with the return button to the previous step which will cause a validation problem.

Concrete explanation with form in ajax and multistep

Indeed Drupal will consider that the field of the current step has not been filled and this will return an error. However, the user wishes to return to the previous step of the form and it is therefore normal that he has not yet completed this field. This therefore poses a fairly serious UX problem because the user expects to be able to return to the previous step.

Here is a concrete example :

The user enters their postal code, goes to step 2 where they are asked for their telephone number. He wants to return to the postal code step and cannot because the telephone field is required.

There is an extremely simple solution on Drupal. We can limit validation on the back button with #limit_validation_errors'

Code #limit_validation_errors

$form['wrapper']['actions']['back'] = [
      '#type' => 'submit',
      '#name' => 'previous-step',
      '#value' => 'Back',
      '#submit' => ['::decrementStep'],
      '#ajax' => [
        'callback' => [$this, 'ajaxFormCallback'],
        'wrapper' => 'form-container',
      '#attributes' =>
      ['class' => ['btn btn-primary btn-prev']],
      '#limit_validation_errors' => [],



#limit_validation_errors : This property is set to an empty array ([]). This means that when submitting the form, this form element (the 'Back' button) will be excluded from validation. This can be useful if you don't want to validate this specific button, as it is intended for a navigation action and not for data submission.

Other useful resources

Add new comment

Similar blog posts

How to delete local changes with git that you haven't committed ?