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
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:
- 3508727-entity-validation-state
changes, plain diff MR !103
Comments
Comment #2
jwilson3Comment #5
benstallings commentedComment #9
dwwThanks! 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