Problem/Motivation

With the location field enabled with dropdown country and dropdown province (which loads the relevant provinces for the selected country) I get an illegal choice has been made error after uploading a photo (though I suspect this issue will occur with any AJAX-ified form element). This only occurs after the country and province have been selected. If I add the photo's first and the location later, all is well.

Steps to reproduce - the setup:

  1. install latest Drupal core (I used 7.25) using standard install profile
  2. download Location 7.x-3.x-dev;
  3. enable Location and Location CCK modules;
  4. add a new Location field to the Article content type;
  5. in the field settings, make sure State/Province is set to: Allow and Drop-down and Country is set to: Allow;

Steps to reproduce - specific steps to get an error:

  1. click Add Content -> Article;
  2. enter 'test' into the Title field;
  3. scroll down to the Location field (created above);
  4. select 'United Kingdom' in the Country drop-down;
  5. wait for AJAX request to complete;
  6. select 'Aberdeenshire' in the State/Province drop-down;
  7. scroll up (or down) to the image upload field (comes with Drupal's standard install profile);
  8. click 'Choose file' and select something from your computer;
  9. click 'Upload';
  10. wait for image to upload over AJAX;
  11. scroll to bottom of form;
  12. click 'Save'.

Proposed resolution

The most obvious thing to do is just fix the issue so Location CCK plays well with other AJAX-ified elements on the same page.

However I'm also linking #2139429: Changing country and then selecting a province returns error "An illegal choice has been detected. Please contact the site administrator." as it has a patch attached that may be relevant to this issue.

Comments

wemmies’s picture

Title: Illegal choice after adding second photo » "An illegal choice has been detected." error on province dropdown after adding second photo
Priority: Normal » Major

Changed the priority to Major as it prevents users from adding info. I've been trying to debug this, but Form API and the Location module just go beyond my knowledge.

wemmies’s picture

I'm trying to build in something like this http://drupal.org/node/339730#comment-2871966 or this http://drupal.org/node/339730#comment-4582034 but I have no joy yet

liam mcdermott’s picture

Title: "An illegal choice has been detected." error on province dropdown after adding second photo » "An illegal choice has been detected." error on province dropdown when other AJAX-ified fields are on form
Version: 7.x-3.0-alpha1 » 7.x-3.x-dev
Issue summary: View changes

Added some instructions to reproduce issue.

Note: I'm not 100% certain this is a general AJAX issue, but that the image upload is an AJAX submit seems very suggestive.

liam mcdermott’s picture

Some notes:

- I put dpm(debug_backtrace())on line 320 of location.module where the value of the select is set. This gave some interesting results (especially as it's called when the AJAX form submit happens, too). This is in the element process function: _location_process_location().

- The state/province dropdown is rebuilt around line 772 of location.module, I've got some lines of debug above that so that's approximate.

- It seems values submitted via AJAX are put (by Drupal) into #default_value instead of #value or value as the code expects on line 321 of location.module.

podarok’s picture

Yup, I`m little pre-occupied(country revolution) and have no free time for fixing this.
I can help with patch creation if in issue someone upload possible fixes or any code gists with problem description via code.
Feel free to upload patch with possible fix and I`ll commit it as fast as possible

gpvdo’s picture

I ran into a very similar problem ('location_element' in Drupal form with Country/Province(select) AJAX update ), and while debugging, ran into the same bug.

1. On/around line 321 of the file location.module (in release 7x.-3.2) there certainly appears to be a 'typo' type bug -
the line should read
if (!isset($element['#value']['country'])) {
instead of
if (!isset($element['value']['country'])) {

2. However, this fix in not sufficient in itself - else one could run into validation errors . One also needs to reset the current state/province choice since the country is changing:
i.e.
change
if (!isset($element['value']['country'])) {
$country = $fdefaults['country'];
}
else {
$country = $element['#value']['country'];
}

to
if (!isset($element['#value']['country'])) { // typo fix
$country = $fdefaults['country'];
}
else {
$country = $element['#value']['country'];
$element[$field]['#value'] = ''; // reset selection to 'Please select' since choices have changed
}

podarok, does the above information suffice for you to build a patch? I have another use case listed in ('location_element' in Drupal form with Country/Province(select) AJAX update ), which required this fix and additional changes.

podarok’s picture

Assigned: Unassigned » podarok
ethan.han777’s picture

um... #6 doesn't seem to work.
I installed new drupal core and location module (7.x-3.2) and I patched the code above.
but I still see the 'An illegal choice has been detected. Please contact the site administrator' message.

podarok’s picture

Assigned: podarok » Unassigned
Status: Active » Needs work

due to #8

ethan.han777’s picture

podarok’s picture

Status: Needs work » Needs review

triggering bot

  • podarok committed d487a9c on 7.x-3.x authored by ethan.han777
    Issue #1804854 by ethan.han777 | wemmies: Fixed "An illegal choice has...
podarok’s picture

Status: Needs review » Fixed

#10 commited
Thanks!!!

Status: Fixed » Needs work

The last submitted patch, 10: location-illegal_choice_error_patch-1804854-9.patch, failed testing.

podarok’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

raycascella’s picture

It looks like the fix here has broken the required settings on the province field, in 7.x-3.x.

By setting the value to #validated, it is then skipping the field for requirement checking completely. It really it should be #processed, as that's what the function is actually doing, not validating it's input. The commit attached to the related ticket should handle both items. After applying the patch and testing, I see no errors with the ajax functionality, and the requirement attribute is still checked.