If the State field uses Autocomplete, then every works fine. The suggestions automatically changes based on the Country selected. However, this doesn't seem to work when using Dropdown. It simply stays at the default Country's States, even when the country changes (the AJAX callback is working, as the State dropdown does fade in and out).

There are also no JS errors on the page itself (confirmed using Webkit Inspector).

Comments

TahitiPetey’s picture

Status: Active » Needs work

Woohoo! I figured out a solution. After trudging through the Location module codes, I found the offending lines of code (lines 311 to 315).

Originally, the code is as such:

if (!isset($element['value']['country'])) {
$country = $fdefaults['country'];
}
else {
$country = $element['#value']['country'];
}

I'm unsure how this could have at any point even worked, there is no ['value'] attribute in the array $element (it's #value, you can see this clearly if you dpm($element);).

The solution is to replace those lines with these:

if (!isset($form_state['values']['locations'][0]['country'])) {
$country = $fdefaults['country'];
}
else {
$country = $form_state['values']['locations'][0]['country'];
}

Understandably, this is more of a customized hack for my particular use case--if you require more than 1 location (and thus there are multiple ['locations'][#] sub-arrays), then this would break horribly.

Nevermind, this solution doesn't pass validation (I guess I counted my chicken before they hatched!). I did find another solution, I'll post it as a patch tomorrow once I have some time.

Sibiraj PR’s picture

same problem here

mstef’s picture

Same problem, but only on the user profile form. It seems to be working fine on node forms.

ss81’s picture

I have the same problem.

Eric B.’s picture

I have the same problem in 7.x-3.0-alpha1.

gpvdo’s picture

Issue summary: View changes

I ran into the same problem and I have it working - see 'location_element' in Drupal form with Country/Province(select) AJAX update

Looks like TahitiPetey identified part of the problem 2 years ago - I am surprised that it has not been fixed yet. He had the correct solution but ran into the validation issue.
I have a fix for the validation issue as well (validation fails because after repopulating state options, the previous selection may no longer be valid) - I have fixed that as well (see above link).

If you try the fix, please post your results.

djdevin’s picture

Status: Needs work » Closed (duplicate)