When the Postal address field is set to be required, and the Default country is set to "- None -", the country actually defaults to the first one (usually Afghanistan), instead of the option "- Select one -".

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

oksana-c created an issue. See original summary.

4p’s picture

I agree with the above

roball’s picture

Title: "-Select country-" as default value for country select if address field is required » Required field does not honor Default country - None -
Version: 7.x-1.1 » 7.x-1.2
Category: Feature request » Bug report
Issue summary: View changes
hkovacs’s picture

I found in function addressfield_default_values that the comment states:
"Fallback to the first country in the list if the default country is not available, or is empty even though the field is required."

I can think of no scenario where this would be relevant upon loading a new form. If the field is required, then like any other form field it should not be auto selected (especially to an incorrect option). 'Required' should be left to form validation and not auto selected.

Looking at the Country field I see that when the default is 'None' the select list contains: - Select a value -

What seems to be going on in addressfield is that the countries are loaded as a select list (in fn addressfield_default_values) without including a '-Select a value-' option which contains a key of '_none'. The likely reason this is not provided is that the validation for required would likely pass if a key of '_none' was provided...

I propose the following on line 381:

  // Set _none as used in Country field
  if (empty($default_country)) {
    $default_country = '_none';
  }

and on line 904:

  $first_element = array('_none' => '- Select a value -');
  $countries = $first_element + $countries;

Now I get the address field country select list displaying similar to the country field.

The only thing that puzzles me is why country field understands and fails validation for value = '_none', but addressfield passes value = '_none'. I know this is probably something simple but I am out of time for now...

@rszrama, hoping for your input on this. Thanks.

roball’s picture

Thanks for your input #hkovacs - hope that a solution will be commited.

sheise’s picture

Running into the same issue.
@hkovacs is right - if the field is required or the previously selected value is no longer available, we shouldn't arbitrarily set it to something else.

Here's a patch to add "Select a value" option when the field instance is required.

sheise’s picture

Status: Active » Needs review
roball’s picture

Status: Needs review » Reviewed & tested by the community

Thank you @sheise, your patch is working fine and solves the problem!

hkovacs’s picture

@sheise thank you.

patch works for me.

roball’s picture

Can we get this patch committed, please?

reszli’s picture

for consistency, this should use theme_options_none()

something like:

if ($properties['empty_option']) {
    $label = theme('options_none', array('instance' => $instance, 'option' => $properties['empty_option']));
    $options = array('_none' => $label) + $options;
  }
DamienMcKenna’s picture

FYI these (and other) errors also show because of bugs in Commerce Paypal, so if you use that module make sure you update to the latest -dev version (or 7.x-2.4 if it's released) to avoid filling your dblog :)

sheise’s picture

for consistency, this should use theme_options_none()

Except that theme_options_none() only supports widget types of options_buttons and options_select but not addressfield_standard so we can't use that.

litnrod’s picture

Patch #6 is currently working great

c7bamford’s picture

Patch #6 adds "- Select a value -" as a country instead of using the empty value attribute for the form. This patch ensures that the empty value is set regardless of the state of the form.

DamienMcKenna’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Status: Reviewed & tested by the community » Needs review

Patch #15 needs a review.

david-urban’s picture

I have tested #15 and it works fine with Drupal Commerce. I'd recommend merging and releasing.