Problem/Motivation
Site owners expect ZIP Codes to be validated, to stop people from entering junk.
Steps to reproduce
Add an Advanced Address element to a form.
Allow the postal code to be filled in.
View the form.
Select US as the country, enter gibberish in the ZIP code field.
Submit the form.
Expected behavior:
* The submission fails because the ZIP code does not validate.
Actual behavior:
* The submission succeeds because the ZIP code is not validated.
Proposed resolution
Use the Address module's validation logic to validate the address.
Remaining tasks
Trigger the module's validation logic.
User interface changes
The ZIP/postal code would be validated for correct syntax when entering an "Advanced Address".
API changes
TBD
Data model changes
TBD
Issue fork webform-3520969
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
Comment #2
damienmckennaRelated issue: https://github.com/commerceguys/addressing/issues/227
Comment #3
damienmckennaDisclaimer: I'm using the address field via a Webform element, so I'm not discounting the possibility that there's a problem with Webform.
Comment #4
damienmckennaComment #5
bojanz commentedThe underlying problem is #2888152: Address validation doesn't run for form elements, only entity fields.
Unfortunately, it never got fixed while Address was more actively developed, so Webform would need to do its own workarond.
Comment #6
damienmckennaThanks for mentioning that issue.
Comment #7
jrockowitz commentedThe workaround is documented here.
Comment #8
jrockowitz commentedThe MR is my best attempt to mock an address item instance and use its validate() method.
The tests provide a good POC, but all improvements are welcome.
I am doubtful #2888152: Address validation doesn't run for form elements, only entity fields will be resolved anytime soon because the complexity of moving validation from the Address item to the element is very challenging, and honestly doesn't make sense.
Comment #10
jrockowitz commentedI ❤️ AI.
I wasn't sure how to trigger address validation via a constraint, and PHPStorm's AI pointed me in the right direction.
I think this is the ideal solution.
A similar solution could be contributed to #2888152: Address validation doesn't run for form elements, only entity fields
Comment #11
jrockowitz commentedOkay, so the AI messed up the code formatting. Everything should be fine now.
Comment #12
jrockowitz commentedTests are passing. This is feeling pretty safe and good improvement.
Comment #14
jrockowitz commented