Entity validation: "State field is required" error for countries with no administrative area

Created on 24 February 2025, about 2 months ago

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.

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

🐛 Bug report
Status

Active

Version

2.1

Component

Code

Created by

🇪🇨Ecuador jwilson3

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

Comments & Activities

Production build 0.71.5 2024