Country field should display the "- Select -" option to match core behavior

Created on 29 August 2018, over 6 years ago
Updated 15 June 2024, 6 months ago

Problem/Motivation

A required Country field does not match core's "List (text)" field's "Select list" widget required field behavior.

When a List (text) field is marked required and the form display widget chosen is "Select list", then the "- Select a value -" option is shown. A required Country field does not show this option.

Note: If this field is overlooked, the user will inadvertently submit a value they did not explicitly choose and may be incorrect.

Original report by edsoncarlos

Add "Select" option if field is required
Add "Select" option in the country list if field is required. But a country still must be selected.

🐛 Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

🇧🇷Brazil edsoncarlos

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom sp3boy

    I am going with #56 which appears to work for my requirements (replacing a basic country selection with an Address field that only captures Country & Administrative Area). Not sure why anything other than "- Select -" would be a better placeholder so not applying #57 myself.

    Thanks for the work!

  • Status changed to Needs review over 1 year ago
  • 🇷🇸Serbia bojanz

    Moving to 2.0.x-dev, as we've closed 8.x-1.x for any risky changes, and this one definitely counts (as evidenced by #45).

    Also, bumping the status back to "needs review". Both #45 and #51 indicate that the patch needs further work (or at least exploration).

  • Status changed to Needs work 12 months ago
  • 🇷🇸Serbia bojanz

    At a glance I can definitely say that we must not modify the address element like this:

    diff --git a/src/Element/Address.php b/src/Element/Address.php
    index b8df3be..f6a55c5 100644
    --- a/src/Element/Address.php
    +++ b/src/Element/Address.php
    @@ -118,9 +118,7 @@ class Address extends FormElement {
         // Preselect the default country to ensure it's present in the value.
         $element['#default_value'] = (array) $element['#default_value'];
         $element['#default_value'] = self::applyDefaults($element['#default_value']);
    -    if (empty($element['#default_value']['country_code']) && $element['#required']) {
    -      $element['#default_value']['country_code'] = Country::getDefaultCountry($element['#available_countries']);
    -    }
    +
    

    This is the big BC break that is breaking Commerce tests.

    For this patch to be committable, it needs to limit itself to the country element/field only, leaving the address UX unchanged.

  • 🇧🇪Belgium nils.destoop

    Re-roll of the patch on the 2.x branch as starting point

Production build 0.71.5 2024