I have a multistep form and on each submit I send completed fields off to an external API which returns data about the next step of the form.
The problem is, if I use form_set_error in the submit handler, the form_storage is cleared and so the form resets to the first page.
Here's a arbitrary example;
function example_form($form, &$form_state) {
if(!isset($form_state['step'])){
$form_state['step'] = 1;
}
$form['test'] = array('#markup' => '<p>Step: ' . $form_state['step'] . '</p>');
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'go'
);
return $form;
}
function example_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
if($form_state['step'] > 5){
form_set_error('test', 'error');
return;
}
$form_state['step']++;
}
So this create a form with just a text block displaying the step number and a submit button that takes you to the next step. Until you go past the 5th step at which point it should start to error.
For some reason in my testing this breaks the step count, and on error takes you back to step 2.
moving the form_set_error into the form callback, or into a validate callback fixes the issue, but it would be cleaner if I could call it in the submit handler.