If I create a multivalue text field with number of values set to "unlimited" and then during form submission I click on "Add another item" button to add new value I get this error "An illegal choice has been detected. Please contact the site administrator." and is not being added.

CommentFileSizeAuthor
#4 illegal choice error.png8.44 KBjesss

Comments

jamiehollern’s picture

I can't replicate this behaviour. Can you post exactly what you're doing? What values are you entering for example?

jamiehollern’s picture

Status: Active » Postponed (maintainer needs more info)
couturier’s picture

I had a similar problem. This old 2009 blog post from Kim mentions a similar problem relating to radio select buttons. I switched my radio select buttons to a drop down select format and it cured my problem. Strangely, the original problem started as I was not allowed to upload an image file, generating the original error. But when I went to Reports > Log Messages, it was giving me the message, "Illegal choice in Blog Tags element." The blog tags select box was inside the custom content type I had created. Changing the select format from radio to drop down helped me be able to fill in all the other fields on the content type, too, for whatever reason.

jesss’s picture

StatusFileSize
new8.44 KB

I'm seeing this behavior as well. I added a new text field to my content type and set it to unlimited. I then went to create a new node with more than one item in that field. When I clicked the "Add another item" button (with the first item populated with "blah"), I got the error message. Twice.

I've attached a screenshot, in case that helps.

jesss’s picture

Version: 7.8 » 7.9

Changing the version number.....

couturier’s picture

This is not going to sound very technical, but it might work. Can you just delete that field entirely and then re-set it up with a few different parameters? Like allow "10" instead of unlimited. Or pick a different way for them to display? Something that simple could solve the problem. It did for me.

jesss’s picture

Status: Postponed (maintainer needs more info) » Active

Setting it to allow 10 items provides text fields for 10 items, as designed. But that's not my use case. The "add another item" button is broken -- that's the bug that needs to be fixed.

couturier’s picture

All right, then, have you gone under Reports > Log Messages right after that happens to see more details about the error? You may be able to find the specific source of the error if you'll look at the full error messages there.

jesss’s picture

There are two error messages written to the log:

  • Illegal choice in Exempt Status element.
  • Illegal choice in Status element.

Those are required List (text) fields. Further testing reveals that if those fields have values selected, the error is not thrown and the "Add another item" button works as designed.

I guess I don't understand why the "Add another item" button is reacting to other, non-related fields.....

couturier’s picture

Yes, that's exactly the same error that was experienced by the person at the blog post I mentioned above. Be sure to read this. Here it is again: Unpuzzling the "An illegal choice has been detected" error.

In that article, notice, "This little piece checks if the passed argument is a valid UTF8 string, and if so htmlspecialchars it, or if not, returns nothing! This causes the return values of our radio button items to have an empty value=”" attribute, causing the validation to fail because of the empty value you posted."

It has something to do with registering an empty value upon selection. Yes, it seems like a bug, but there should be a way around it. Maybe you can set a default value to fill the place just so no empty value is ever detected. For me, changing from radio buttons to a drop-down selection list in my Custom Content Type did something to eliminate the empty value from posting. And yes, just like you are experiencing, the thing that was so weird was that my problem was in uploading an image file. That's what returned the error. But it was fixed when I changed the selection method of another, non-related field, a tag selection drop-down menu. Do you have radio buttons anywhere in your Custom Content Type? If so, try changing them to drop-down selection.

Let us know if you can figure out where your empty value is coming from. We need to get this figured out for future users, too.

jesss’s picture

Thanks for the link, but I don't think that blog post quite fits this issue.

Debugging this at the code level is beyond my skill set, but here's what I'm seeing on the front end:

When the Text field "Add another item" button is clicked, the wrong form elements are being validated.

Specifically, it's validating ALL required List (text) fields -- List (integer) and List (float) fields are not affected, and neither are non-required List (text) fields.

If clicking the "Add another item" button is supposed to trigger some sort of validation (and I'm not sure why it should, though I'm certainly no expert), shouldn't it only validate the associated Text field?

couturier’s picture

I'm not sure the answer to your question, but I have wondered if maybe the order of your fields would make any difference. Can you try putting all your required list fields before the "Add another item" button to see if that makes any improvement? I'm just guessing at this point. You would need someone more experienced to reply to this post if experimenting with setting the options on your fields selections does not work like it did for me. If you have tried everything else, what I would recommend is changing the status of this Issue to "Support Request" under Category in order to catch the attention of someone else who might help. Also, have you tried posting the question in IRC channels? You can connect online through http://webchat.freenode.net and join any one of the following groups: http://drupal.org/irc

jesss’s picture

No, re-ordering the fields has no effect. This is truly a bug.

Thanks for your advice today.

jesss’s picture

Title: "An illegal choice has been detected. Please contact the site administrator." while trying to add new field value » Text field "Add another item" button validates unrelated fields, throws errors

Editing the issue title for clarity.

couturier’s picture

Component: forms system » field system
Category: bug » support

All right, then, jesss, I'd have to agree with you that it is abnormal behavior and we should not have to be doing these weird things to work around it. If you don't mind, we might also change the status of this thread to "support request" in hopes that someone else will pick up and contribute an answer quickly in the event that it may still be something that can be solved on the user end. If not, then someone should change this back to "bug report" and wait months for a review. I've also changed the issue to "field system" component, which I think is more accurate. In the meantime, I hope you can figure out something that works for your application. Best wishes.

stevieb’s picture

I've had exactly the same issue relating to required "radio select buttons"

the errors do not occur when the field settings have a default value
or are changed to "select list"

couturier’s picture

That is, in summary, exactly what solved it for me, too. Jesss, are you absolutely sure that you tried to switch your radio buttons to a select list instead?

jesss’s picture

Category: support » bug

@couturier, I appreciate that you're trying to find a workaround. I'm using a workaround myself for my particular situation. However, workarounds don't solve the root problem -- a bug in the code.

The "add another item" button works when the the list (text) field is not required, or has a default value, or is a select list instead of radio buttons.

The "add another item" button does not work when the content type includes a required list (text) field using radio buttons that has no selected value.

That's the bug that needs to be fixed.

Anonymous’s picture

I have the same problem. I created a simple form with radio buttons the user can select. If the field is required and nothing is selected I get this error: An illegal choice has been detected. Please contact the site administrator.
Log shows: Message Illegal choice in Choices 1 element.

Here what is in my form:

$form['choicetxt'] = array(
      '#type' => 'radios',
      '#title' => t('Choices 1'),
      '#title_display' => 'invisible',
      '#options' => $list,
      '#required' => TRUE,
    );

same issue with or without #default_value.
same issue with or without #input = TRUE.
I even copied the The form['xxxx' ... part from an other form which works without problems and shows when nothing has been selected an normal error message to inform the user that the field value is missing. The only difference is that the other form has been created with an earlier Drupal 7.x version. I also see that the working form does not have set #needs_validation while validating the radios and therefore does not go into the follwing if statement which is also in form.inc on line 1270. The working form then goes to my own validation function where I set the error message for the missing input.

    if (isset($elements['#needs_validation'])) {

The error applies before the own added validation function applies. This means I can't use a validation function as a work around.
It seems that empty radios are not handled.

debugger jumps within form.inc
on line 1283

if (is_array($elements['#value'])) {   

directly to second elseif on line 1305

elseif (!isset($options[$elements['#value']])) {
          form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
          watchdog('form', 'Illegal choice %choice in %name element.', array('%choice' => $elements['#value'], '%name' => empty($elements['#title']) ? $elements['#parents'][0] : $elements['#title']), WATCHDOG_ERROR);
        }

Here the elements variables.

$elements	Array [24]	
	#type	radios	
	#input	true	
	#title	Choices 1	
	#title_display	invisible	
	#options	Array [4]	
	#required	true	
	#process	Array [1]	
	#theme_wrappers	Array [1]	
	#pre_render	Array [1]	
	#defaults_loaded	true	
	#tree	false	
	#parents	Array [1]	
	#array_parents	Array [1]	
	#weight	0.002	
	#processed	true	
	#attributes	Array [0]	
	#id	edit-choicetxt	
	#name	choicetxt	
	#needs_validation	true	
	#value	
		length	0	
	9	Array [24]	
	10	Array [24]	
	8	Array [24]	
	11	Array [24]	
xjm’s picture

Status: Active » Closed (duplicate)

This seems to be the same issue as #1162712: AJAX submissions try to validate other form fields: "An illegal choice has been detected.". Marking this as a duplicate of that.