I get the error "Illegal choice" when I submit a form with a multi-select option that was freshly created via ajax. Workflow is like this:

  1. User loads node edit form
  2. User needs to select a non-existant job title (job titles are drupal taxonomy terms)
  3. User presses my custom ajax button that (1)takes their new title, (2) adds their new title as a drupal term, (3) updates the form in the browser, (4) selects the new title

When I submit the form, drupal doesn't seem to see that the new title has a corresponding (therefore valid) TID and errors out in the validation.

I tried creating custom validation on the title element, but despite passing that validation, drupal still does its own check, which fails.

Anyone know how to work around this?

1 Answer 1


Drupal checks the integrity of the form to make sure submitted values match up with the available options when the form was built; it's a security feature. Manipulating the form on the client-side, without informing the backend that something has changed, causes the integrity check to fail.

Rather than work around it (which is possible but messy), I'd recommend using the AJAX functionality already built into the form API.

The AJAX Forms in Drupal 7 page, FAPI AJAX reference and the AJAX Example in the Examples module are excellent resources for learning about this.

  • I am trying to get the #ajax approach working, but this case is a complicated one to learn on (need a text field to open in a lightbox, submit & save new term, then update the form in the browser). This is further obfuscated because I am altering node-edit form via an #after_build function. I need this done asap & his may not be the time/place to learn this so I'd be interested in alternatives..
    – doub1ejack
    May 2, 2013 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.