Problem/Motivation

Countries that have no concept of a state or province, a.k.a. "Administrative Area", fail Entity validation when Country and State fields are marked required on Address field configuration.

Steps to reproduce

1. We have a REST resource for allowing account creation from federated headless subsites, to a main Drupal website, where user accounts are housed. User accounts have an Address field with Country and State (i.e., administrative_area) fields required.

2. User registers on the subsite and selects Sweden for country. The "State" field is either removed or disabled.

3. JS submits to /user/register?_format=JSON with the user account information payload, leaving state either as an empty string, or completely absent from the JSON.

4. The REST response is an UnprocessableEntityHttpException, generated from the EntityResourceValidationTrait::validate() function as called from inside core's UserRegistrationResource::post() function ($this->validate($account);)

Unprocessable Entity: validation failed.
field_address.0.administrative_area:  field is required.

Proposed resolution

Ensure that entity validation of Address fields take into account the selected Country, and adjust the dependent address field validations accordingly. Eg, if administrative_area is supposed to be empty for a given country, do not send a validation error when the value is left empty. Instead, send a validation error when the value is non-empty.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork address-3508727

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jwilson3 created an issue. See original summary.

jwilson3’s picture

Issue summary: View changes

benstallings made their first commit to this issue’s fork.

benstallings’s picture

Status: Active » Needs review

dww made their first commit to this issue’s fork.

  • dww committed 29c3313e on 2.1.x
    fix: #3508727 Entity validation: "State field is required" error for...

  • dww committed e2d7cc32 on 2.0.x
    fix: #3508727 Entity validation: "State field is required" error for...
dww’s picture

Version: 2.1.x-dev » 2.0.4
Status: Needs review » Fixed

Thanks! I carefully reviewed this. Didn't spot anything worth complaining about. Rebased to latest 2.1.x. Pipeline is all good:
https://git.drupalcode.org/project/address/-/pipelines/1023651
Test-only fails as expected:
https://git.drupalcode.org/project/address/-/jobs/13230551

Merged to 2.1.x, cherry-picked to 2.0.x and pushed. This will go out with 2.0.5, which I'm hoping to tag momentarily.

Thanks again!
-Derek

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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