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

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

very useful. thanks. for

very useful. thanks.

for some reason when I switched to using my own theme for admin the filter option radio buttons for the text areas had been cleared. Once I selected the correct filter for each one again all was fine. phew!

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.