Hi,

I'm trying to create a multistep form using the form wizard and I'm a bit stuck on how to get the back button to work for me in my case. Basically if I go to, say, step 2 which has required text field and want to go back to step 1 it won't let me as the textfield has no value. In the advanced help it talks about looking in the cache for a previously set value for proper handling of the back button, but this doesn't apply as no value has been set yet.

For the moment I'm setting my default to something obvious if there's nothing in cache (i.e. "[Enter Value]") and in the validate function for the form I start it with a check for the op and then if the value has my obvious value in it fail the validation.

>
  if ($form_state['input']['op'] != 'Back') {
    if ($form_state['values']['company_name'] = '[Enter Value]') {
      form_set_error('company_name', 'Company Name field is required');
    }
  }

This feels a bit hacky though so thinking I'm missing the more elegant solution somewhere. Any ideas?

Thanks,
Anthony.

Comments

merlinofchaos’s picture

Status: Active » Closed (works as designed)

Form API has no way to skip processing of the #required attribute, which unfortunately means that it cannot be used in a form wizard if you want the back button to work. I know, it is ridiculous, but true.

The recommendation is that you do not use #required, but instead do what #required would do in your own validation routine.

It is with great sadness that I mark this 'by design'. =(

malks’s picture

Thanks for the background, I suspected that would be the case. I've hit issues with #required since then and have come to my own conclusions about its value :) I'm finding that it's easier to not use #required beyond simple use cases in a few situations.

thanks,
Anthony.

merlinofchaos’s picture

I may have found a way around this with recent changes to the ctools_build_form() path where I took control of that from FAPI and added a way for wizard.inc to, I think, completely disable validation. I'm not sure, though, as #required may be validated differently.