-
Notifications
You must be signed in to change notification settings - Fork 219
Only validate postcode if its required or filled #9377
Conversation
The release ZIP for this PR is accessible via:
Script Dependencies ReportThere is no changed script dependency between this branch and trunk. This comment was automatically generated by the TypeScript Errors Report
🎉 🎉 This PR does not introduce new TS errors. |
Size Change: +355 B (0%) Total Size: 1.11 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this, @senadir.
Testing optional empty fields
While looking into the first test case Testing optional empty fields
, I noticed that the postcode validation error still appeared when applying the following steps:
- Go to
/wp-admin/admin.php?page=wc-settings
and setDefault customer location
toNo location by default
. - Open an incognito window.
- Add a product to the cart.
- Go to the checkout page.
- Provide the following credentials
Email address: [email protected]
First name: Jaap
Last name: Schippers
Address: Main Road 23
Country/Region: Vietnam
Postal code (optional): KEEP EMPTY
City: Hanoi
Phone (optional): KEEP EMPTY
- Click on
Place Order
. - See that postal code validation kicks in and inline error message
Please enter a valid postal code
is visible. - Refresh the page.
- Click on
Place Order
. - See that the order can be placed now.
- Repeat steps 2. until 7. with a fresh incognito window.
- Instead of refreshing the window, add some characters in the postal code field and empty the field again.
- Click on
Place Order
. - See that the order can be placed as well.
Testing optional filled fields
While looking into the second test case Testing optional filled fields
, I saw the same behaviour as for the first test case. These are my testing steps for the second test case:
- Go to
/wp-admin/admin.php?page=wc-settings
and setDefault customer location
toNo location by default
. - Install and activate the plugin
Code Snippets
. - Go to
/wp-admin/admin.php?page=add-snippet
and add the following snippet:
add_filter(
'woocommerce_get_country_locale',
function( $locale ) {
$locale['GB']['postcode']['required'] = false;
return $locale;
},
10,
1
);
- Ensure that the priority for the snippet is set to
1
:
- Open an incognito window.
- Add a product to the cart.
- Go to the checkout page.
- Provide the following credentials
Email address: [email protected]
First name: Jaap
Last name: Schippers
Address: Main Road 23
Country/Region: United Kingdom (UK)
Postal code (optional): KEEP EMPTY
City: London
Phone (optional): KEEP EMPTY
- Click on
Place Order
. - See that postal code validation kicks in and inline error message
Please enter a valid postal code
is visible. - Refresh the page.
- Click on
Place Order
. - See that the order can be placed now.
- Repeat steps 2. until 7. with a fresh incognito window.
- Instead of refreshing the window, add some characters in the postal code field and empty the field again.
- Click on
Place Order
. - See that the order can be placed as well.
) | ||
field.required || inputObject.value | ||
? customValidationHandler( | ||
inputObject, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these checks should be inside the customValidationHandler if possible. The customValidationHandler
should receive another parameter isOptional
, defaulted to false and should handle the logic inside.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the fix! It works great now. 🎉
* Only validate postcode if its required * Clear postcode validation error if postcode is not required --------- Co-authored-by: Niels Lange <[email protected]>
We are validating postcodes even if they're not required, in this PR, I change that so we only validate them if they're required or filled.
The filled check prevent adding invalid optional postcodes.
Fixes #9376
User Facing Testing
Testing optional empty fields
Testing optional filled fields
For this one we need a turn a required country to optional, this can, add this code to make the UK's postcode optional
Changelog