I am using the Xajax module to change the available options of a second listbox according to the choice the user makes on the first listbox, e.g. when the user chooses a country, the province listbox options are updated accordingly.

With Drupal 4.6, I can do this without any problem. However, with Drupal 4.7, the target listbox option change works as expected, but when I click 'Submit', an error message "An illegal choice has been detected. Please contact the site administrator." occurs.

Could someone guide the way for correcting this problem.
Thank you.

Comments

fool2’s picture

Sounds like a bug in the code, but I'm not familiar with the module you're using.

Illegal choice errors happen mostly when the value of the form doesn't match up to the form when it is loaded on the next page.

This is a security measure from forms API designed so people can't send bogus values to select fields. It validates on the page that is receiving the form values, however, so in your case the form would need to be loaded as the page is loading instead of by AJAX.

I'm not sure how deep you are in the code, but that is what's causing the error.

xbc’s picture

As a temporary fix, I disable the error-checking by commenting out Lines 222-223 of form.inc, that seems to work fine. Basically, I think the problem lies on validating the input of an element when the selected value is only produced (by AJAX) after the form has been rendered.

fool2’s picture

That's what I was trying to say.

What you need to do is have the form rendered based on the variable passed from the first form. You should be able to do that based on the first form value, the one that you use to select the AJAX form.

If you are working with forms API your target form should have a switch inside the options field like this one:

'#options' => $state ? _get_cities($state) : array('0'=>'State not selected'),

In this example, $state is the value given by the first select to determine the second select.

The tertiary (?) operator is great for this because you can switch #options depending on whether state is set or not.

_get_cities would return the same values that your AJAX would, except as an associative array.

JamieR’s picture

There is a form element you can set to disable the validation in 4.7. It's discussed here: http://drupal.org/node/52640

Example:

$form['dynamicselect'] = array(
    '#type' => 'select',
    '#title' => t('Please select from this dynamic list'),
    '#options' => array(),
    '#DANGEROUS_SKIP_CHECK' => TRUE,
);

Hope that helps.

alexis’s picture

I was getting some values for my select element using Javascript and had the "illegal choice" error bothering, the code you suggested fixed it.

Thanks!

Alexis Bellido
Ventanazul: web development and Internet business consulting shouldn't be boring

kanirudha2008’s picture

I was also facing the same kind of problem. I changed my code to incorporate the new changes and it fixed the problem.
Thanks for the comments.