Drupal 6, avoiding message ‘An illegal choice has been detected’.

Drupal form control can be tiresome some times, especially when developping custom modules .. In my case it was the integration of country and region select boxes from the ubercart module.

In my implementation of hook_form_alter we are adding these two elements in code :

$select_country = uc_country_select(uc_get_field_name('country'), $country, NULL, 'name');
$select_zone = uc_zone_select(uc_get_field_name('zone'), $zone, NULL, $country, 'name');

$form['country'] = $select_country;
$form['zone'] = $select_zone;

But when submitting the form we have always the same error : An illegal choice has been detected ??!!

As a solution and reading validation from code it’s possible to add in the form alter the information that this form has been already validated :

$form['zone']['#validated'] = true;

8 Replies to “Drupal 6, avoiding message ‘An illegal choice has been detected’.”

  1. Hi
    Thanks a lot for the wonderful hack.
    I was doing all sort of AJAX and AHAH stuff and finally your post solved that all.

    Thanks again

  2. Hi,

    Thanks a lot for this simple line of code I was looking for 🙂

    I had two dropdowns, if the user changes the first one the options of the second are filled in with relevant data. However, I’ve done this using jquery and drupal json. I was surprised that I’ve got this error when submitting the form.

    Thanks a million!

Leave a Reply