Hi,
The previous button in wizard example won't work if the form contains a required 'radios' form element which has no selected option.
It seems that previews submit is being validated although
'#limit_validation_errors' => array()
is set on the previews submit button element.
I get this error when trying :

An illegal choice has been detected. Please contact the site administrator.

To reproduce the problem, add the following code to 'form_example_wizard_location_info' function:

$form['test'] = array(
    '#type' => 'radios',
    '#title' => 'Test',
    '#options' => array(
      '1' => '1',
      '2' => '2',
      '3' => '3',
    ),
    '#required' => TRUE,
  );

Then go to the second step of the wizard in 'examples/form_example/wizard', then without selecting any option of the radios element, click on the previous button.

Comments

rfay’s picture

Patches are welcome. Please sort out whether you think this is a Drupal core issue or an error in this example.

farhadhf’s picture

I'm not sure, But after some digging It seems to be related to the core form API... line 1308 of form.inc, in _form_validate()

        // Non-multiple select fields always have a value in HTML. If the user
        // does not change the form, it will be the value of the first option.
        // Because of this, form validation for the field will almost always
        // pass, even if the user did not select anything. To work around this
        // browser behavior, required select fields without a #default_value get
        // an additional, first empty option. In case the submitted value is
        // identical to the empty option's value, we reset the element's value
        // to NULL to trigger the regular #required handling below.
        // @see form_process_select()
        elseif ($elements['#type'] == 'select' && !$elements['#multiple'] && $elements['#required'] && !isset($elements['#default_value']) && $elements['#value'] === $elements['#empty_value']) {
          $elements['#value'] = NULL;
          form_set_value($elements, NULL, $form_state);
        }
        elseif (!isset($options[$elements['#value']])) { // here
          form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
          watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
        }

Probably depending on '#limit_validation_errors' value of the submit button an appropriate value must be sent to form_error function as the 3rd parameter ?

rfay’s picture

Well, this bug report isn't certainly about the Examples project... it's either about your module or Drupal core. Thanks for looking into it and pursuing it.

farhadhf’s picture

This happens when I put the code I provided in Issue summary in form_example.module, and the code does not seem to be wrong...
I created an issue on core form system.

Sorry for wasting your time :)

mile23’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

Core issue is close, marking this closed.