-
Notifications
You must be signed in to change notification settings - Fork 219
Client side postcode validation #7722
Comments
Earlier attempt in validating postcodes on client-side: #4014 |
#6989 issue could also be resolved when we introduce client-side validation. |
postcode validation is extensible on PHP, so there are a couple of things to consider here:
|
I believe we should serialise the rules, not the regex. e.g. this country has digit limit of 2. Then we can handle the implementation details in JS/PHP separately.
I have a way to funnel errors from the API to the field validation - just haven't raised a PR yet. |
So you want to translate rules to a regular expression? |
Given that some folks complaints about performance in the past, I wonder if it isn't a faster approach to validating the postcode client-side using a TS-function. As for the current PHP-based validation, I noticed that many countries are not validated, e.g. Australia. Within the core validation of Woo, I also noticed this fallback value: I might be mistaken, but to me the current postcode validation from Woo core looks a bit incomplete. |
Client-side validation is the idea here — this is the main pain point (invalid data is being sent to the server, which fails the server side validation). We need parity with the validation functions in core.
It needs to default to true to accommodate countries with no rules or incomplete rules.
Thats an option yes. But to avoid core refactors we can probably just aim for parity and log an issue for the future to share the same system. Any "custom" postcode rules will just come through via the API, if data is not valid. |
Ah, I see. Thanks for clarifying this. |
I looked into a few different external libraries, and the majority are indeed regex-based, so a similar approach as woo core and the earlier attempt. Examples:
I looked to see if Stripe had a validation library for this, but I couldn't find anything. In the docs, they do say they validate numeric vs alphanumeric codes based on country. Let's go with regex in the client. If we cover only the countries listed in core on the PHP side, that should be enough. Also, if we could get the GB postcode format done in regex, so all rules are consistent, that might also be a good idea instead of having dedicated functions for certain countries.
|
Those are the 2 points I have in mind, having rules extensible isn't ideal (I think it might be the wrong approach). The use case I'm afraid of is when a merchant uses code to remove validation for a country so now you have a client that is overly stricter than it should be. However, I think this can be solved in the future by code as well, you can (using a simple woocommerce core hook) make the postcode not required for a country and that should solve it. |
The code to loosen up client requirement is this: add_filter(
'woocommerce_get_country_locale',
function ( $countries ) {
$countries['DZ'] = array(
'postcode' => array(
'hidden' => true,
'required' => false
),
'address_1' => array(
'hidden' => false,
'required' => false
)
);
return $countries;
},
1,
10
); |
We need to implement postcode validation in the client to prevent erroneous requests to the server. Postcode validation rules can be found in core/Store API.
The text was updated successfully, but these errors were encountered: