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']]
];