Add a setting for disabling postal code validation

Created on 30 October 2019, over 4 years ago
Updated 10 March 2024, 4 months ago

First of all: thanks for all the great work on this module, really helps me forward, both on commerce and non-commerce projects.

I just talked to Ryan at Drupalcon Amsterdam.
It would be nice to have the option to disable the postal code validation for certain use cases. I guess it would be a checkbox that turns off validation for the field.
I don't know what validation is active on the other address fields, but I ran into this issue with German postal codes that need a 'B-' before 5 digits.

In some cases, it isn't necessary to have validation on the address field at all.
Also: I'm not sure what the validation error is right now, but it would be nice to have a meaningful validation error for each country (for example: Please fill in the postal code in the following format: 'B-12345'.

Thanks for looking into this. I don't think the other issues are exactly this, but if so: please redirect me to the right issue.

✨ Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡§πŸ‡ͺBelgium pietermouton

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    This would actually still be useful.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States dww

    Thanks for keeping this moving. However, back to needs work for the following:

    1. Still needs tests (per #8).
    2. Still needs an upgrade path (also per #8).
    3. A "disable X" checkbox is an anti-pattern for a UI. You should check a box when you want something to happen, not when you want it not to happen. The patch in #5 got this right: it was a setting called "Validate address format"m that defaulted to TRUE, that you had to uncheck if you wanted to disable validation. Patch #9 inverted it and the later patches continued the inversion. We need to re-invert this back to "Validate postal code" or something.

    Thanks again!
    -Derek

  • πŸ‡ΊπŸ‡ΈUnited States bogdog400

    This would be useful to me too. I'm getting grief for some postal codes now. Specifically US codes with more than 5 digits.

  • πŸ‡ΊπŸ‡ΈUnited States bogdog400

    I had quite a bit of trouble with this validation because I was using the JSON API and that's a bit harder to debug. I'm including this note just in case it helps someone else.

    This is logging code wouldn't work for me when I inserted it in core/modules/jsonapi/src/Exception/UnprocessableHttpEntityException.php

    
     \Drupal::logger('myerror')->warning('This is my error:'. print_r($violations) );
    

    But this did:

     \Drupal::logger('myerrror')->warning('This is my error:'. $this->$violations->getFieldNames() );
    

    It didn't generate the kind of log that I wanted, though. But the log message said that "getFieldNames" didn't exist and then it included a nice print out of the rest of the object.

    I'm fully aware that i wasn't doing the right thing with the PHP, but it was rather maddening and I'm leaving this as a potential help for others who experience it.

  • πŸ‡§πŸ‡ͺBelgium RandalV

    Refactored against 1.x-dev

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    25 pass, 2 fail
  • πŸ‡§πŸ‡ͺBelgium RandalV

    Ugh, seems like I forgot to add some use statements in the previous patch, apologies.
    Attached should be the fully working patch.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    32 pass
  • Assigned to sickness29
  • Issue was unassigned.
  • Status changed to Needs review 7 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update 7 months ago
    36 pass
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Hello there,
    I took patch from #5 as base and added post_update hook for existing address fields and also added address validation test.
    I have also added similar test here https://www.drupal.org/project/address/issues/2994943#comment-15323637 ✨ The address element should strip trailing whitespace from input Needs review , so would be good to combine those into one and move all data to single data provider. Let me know what you think

  • Status changed to Needs work 7 months ago
  • πŸ‡·πŸ‡ΈSerbia bojanz

    As mentioned in #14, this setting should be "validate_postal_code", not "validate_address_format".

    The user cares about not validating postal codes, they don't even know what an address format is.

    We will also need to add a setting to the AddressFormatConstraintValidator in commerceguys/addressing that allows postal code validation to be turned off, then pass that setting along. We can't just turn off the entire validator because that would be a problem for REST users (which would lose required field validation, subdivision validation, etc).

    Question: Do we actually need an update hook if we're setting the default value? Shouldn't Drupal be always adding the default value for us?

  • πŸ‡·πŸ‡ΈSerbia bojanz

    Bumping to 2.0.x since this will require a library update.

  • Status changed to Needs review 6 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 6 months ago
    38 pass
  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Hi @bojanz
    I have renamed the setting and applied code changes to 2.0.x.
    Added new setting to constraint as well.
    Tested with existing field and getSetting only looks for field config values, defaultFieldSettings is only used when new field is created, so I left post update function to update existing field config.

  • πŸ‡·πŸ‡ΈSerbia bojanz

    Patch looks good, now we need to implement the library side.

    Leaving that on my plate for 2024.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 6 months ago
    47 pass
  • πŸ‡¬πŸ‡§United Kingdom jofitz

    Patched from #23 no longer applies.

    Re-rolled

  • πŸ‡·πŸ‡ΈSerbia bojanz

    Updating version. We will commit this for Address 2.1.0.

    I've done what I promised in #24, the library has been fixed, now it just needs a new release.

    • bojanz β†’ committed 1dc5ab26 on 2.x
      Issue #3091072 by RandalV, andrewmacpherson, sickness29, jdleonard,...
  • Status changed to Fixed 4 months ago
  • πŸ‡·πŸ‡ΈSerbia bojanz

    Thanks, everyone!

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

Production build 0.69.0 2024