Debugging the Mysterious "An illegal choice has been detected. Please contact the site administrator." Error

The Drupal Form API is a wonderful thing... once you've been using it long enough, it's easy to forget all of the laborious validation and post-submit processing you used to do by hand for each and every HTML form in your solution.

The FAPI does things its own way, though, and if you're building especially complex forms that involve multiple steps or dynamically generated options, you may encounter the following error upon submitting your form:

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

This is Drupal's way of saying "Hey, you tried to submit this form with a <select>, checkbox or radio button option that wasn't included in the original form definition! That's not allowed."

If you go digging for the source of this error message in the Drupal 5 core you'll find it in the _form_validate function. You'll also notice that immediately after the cryptic end-user message is set, Drupal makes a Watchdog entry. Assuming you're logged in with administrator privileges, you can go to Administer > Logs > Recent log entries (admin/logs/watchdog) and see exactly what the offending option was, and which form element it applies to:

Watchdog FAPI error detail

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

could also be...

This could also be an indiciation of:

"Hey, you looked at this form (which generated the form token based upon your session) and then regenerated your session by logging out/in or something like that and then tried to submit the form by which point the form token is no longer valid since logging in again has changed your session data"

I get that message fairly often on Groups.Drupal.org

OK, so how does one fix this?

If I am using dynamic select boxes and I don't want all the values to be in the drop-down initially, how do I get around this?

See here:

See here: http://drupal.org/node/153774 for solution

My Resolution

This was happening to me in a custom module I was developing. I checked the source code of the form that was throwing the error to find out there were line breaks in my values.

Example:

<select><option value="Some Value
"></select>

Solution:
I simply used the php trim() function around my values to remove the line breaks.

Hope this helps someone :)

Shawn

Thanks!

Thanks! trimming solved it for me

Obscure edge case

I determined that if you are submitting a form programmatically using drupal_execute() & you've built the form array yourself, invalid values for default form elements can also cause this error.

For example, in my case, I had set the input format to 5, but I was on a site that didn't have an input format 5. Thus, the input format element was getting the text "More information about formatting options" passed to it as a choice. This, of course, was invalid.

Using the dblog enabled me to debug this, but I had to filter it down to form errors first since I get such a large number of errors on my heavily-trafficked site (sad, I know).

Solved the illegal choice issue!

Hi people, I've been dealing with this error when I upgraded some client websites to D6.13. Suddenly the error popped up all over the place on different pages (and forms) so I tracked it down to the form api and bootstrap... I know people should not be making adjustments in bootstrap.inc but it's the final endpoint of this error. Feel free to check out my solution at http://www.halecommunications.be/blog/?p=88, I hope it isn't a problem to paste my article here :) Kind regards, Kim

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p><div> <br><img>
  • Lines and paragraphs break automatically.

More information about formatting options

Verification
This question is for testing whether you are a human visitor and to prevent automated spam submissions.