0

I'm working on a D8 project and I have a multistep webform. Everything works fine but when I click the previous button I'm getting errors that the required fields shouldn't be null in order to go to the previous step. Is this a configuration I need to change or is there a way to avoid it?

Thanks in advance.

2 Answers 2

1

For exactly this purpose the form has the #limit_validation_errors attribute. It is an array of values that will be validated instead of the whole form.

So to go back this woul mean an empty array since you do not want to valdiate anything.

But a small issue is that with empty array you will not be able to detect the button that submitted the form so you have to always include it in the list. If you have custom button #name use that, if not then use op which is the default.

Also these are the #parents values, not #array_parents so if you have a tree form you have to take that into account. Also using this will not only limit the validation, it will actually strip the submitted values, so if you want to go back but save some information(textfield) from the form, you have to include it as well.

For example:

$form['actions']['back'] = [
  '#type' => 'submit',
  '#value' => $this->t('Back'),
  '#submit' => ['::goBackSubmit'],
  '#limit_validation_errors' => [['op']]
];

or:

$form['actions']['back'] = [
  '#type' => 'submit',
  '#name' => 'go_back_submit',
  '#value' => $this->t('Back'),
  '#submit' => ['::goBackSubmit'],
  '#limit_validation_errors' => [['go_back_submit']]
];

or:

$form['#tree'] = TRUE;
$form['actions']['back'] = [
  '#type' => 'submit',
  '#name' => 'go_back_submit',
  '#value' => $this->t('Back'),
  '#submit' => ['::goBackSubmit'],
  '#limit_validation_errors' => [['actions', 'go_back_submit']]
];
1
  • Thank you. Should I check that in the form_alter? It's not a custom form but a webform.
    – Ismini
    Aug 11, 2017 at 15:11
0

I found the answer in the module.

Initially, the module doesn't validate when the user previews the form. In order to do that uses the noValidate function.

So what I did was, in hook_form_alter, to check if I have the previous button and replace the validation function:

  if(isset($form['actions']['wizard_prev'])){
    $form['actions']['wizard_prev']['#validate'] = ['::noValidate'];
  }

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.