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

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

damienmckenna created an issue. See original summary.

damienmckenna’s picture

damienmckenna’s picture

Disclaimer: I'm using the address field via a Webform element, so I'm not discounting the possibility that there's a problem with Webform.

damienmckenna’s picture

Title: Validate US ZIP codes » Validate US ZIP codes in the Advanced Address element
Project: Address » Webform
Version: 2.1.x-dev » 6.3.x-dev
Issue summary: View changes
bojanz’s picture

The 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.

damienmckenna’s picture

Thanks for mentioning that issue.

jrockowitz’s picture

jrockowitz’s picture

Status: Active » Needs review

The 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.

jrockowitz’s picture

I ❤️ 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

jrockowitz’s picture

Okay, so the AI messed up the code formatting. Everything should be fine now.

jrockowitz’s picture

Tests are passing. This is feeling pretty safe and good improvement.

jrockowitz’s picture

Status: Needs review » Fixed

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

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

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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