Describe the bug
It is not really a bug but more of an issue in order to start a discussion why the Zip Code Validation is written is this way. When using the Zip Code Format validation, Spaces and Hyphens are translated to the next Regex
$zip_regexp = str_replace(' ', '( |)', $zip_regexp);
$zip_regexp = str_replace('-', '(-|)', $zip_regexp);
This gave me some problems with validation Canadian Zip Codes because I really need the space. Unfortunately this regex allows the user to enter a zip code without any spaces.
To Reproduce
Steps to reproduce the behavior:
Link to the code : https://github.com/PrestaShop/PrestaShop/blob/e12f2cb249611fcf4a9516d47ab1b13314c68bc7/classes/Country.php#L451
Hi @jojotjebaby,
Thanks for your report.
I manage to reproduce the issue with PS1.7.5.1 & PS1.7.4.4.
In the BO => International => Locations => Countries
I edit the Zip/postal code format

In the FO, I can add a postcode without space successfully.
https://drive.google.com/file/d/1y02i96hq5ksfWGXl1sAlNEd6iZZiL6v_/view?usp=drivesdk
I鈥檒l add this to the debug roadmap so that it鈥檚 fixed. If you have already fixed it on your end or if you think you can do it, please do send us a pull request!
Thanks!
I already made a fix and will open a PR today or tomorrow.
Thanks!
@jojotjebaby, thank you!
Important in 1.7.6 this fix is not good. In The Netherlands we have zip codes like : 7607 NC or 7607NC (with or without space). Now the validation says 7607NC = invalid. This merge needs to be undone. @Quetzacoalt91 @marionf
Important in 1.7.6 this fix is not good. In The Netherlands we have zip codes like : 7607 NC or 7607NC (with or without space). Now the validation says 7607NC = invalid. This merge needs to be undone. @Quetzacoalt91 @marionf
Thank you for your reply. I think we need to rework this in order to allow multiple formats to be submitted to the zip code verification. What do you think ?
Hi, yeah. I fixed it in our 1.7.6 case with this:
Now in /classes/Country.php:
$zipRegexp = '/^' . $this->zip_code_format . '$/ui';
$zipRegexp = str_replace('N', '[0-9]', $zipRegexp);
$zipRegexp = str_replace('L', '[a-zA-Z]', $zipRegexp);
$zipRegexp = str_replace('C', $this->iso_code, $zipRegexp);
Changed to (code from 1.7.4):
$zipRegexp = '/^'.$this->zip_code_format.'$/ui';
$zipRegexp = str_replace(' ', '( |)', $zipRegexp);
$zipRegexp = str_replace('-', '(-|)', $zipRegexp);
$zipRegexp = str_replace('N', '[0-9]', $zipRegexp);
$zipRegexp = str_replace('L', '[a-zA-Z]', $zipRegexp);
$zipRegexp = str_replace('C', $this->iso_code, $zipRegexp);
This fix was to apply a more strict verification for the zip codes. Going back to the 1.7.4 code would mean that spaces in the verification string would be ignored...
Would there be a possibility to expand this feature in order to allow multiple zip verification strings in order to let the store owner choose what zipcodes he allows.
example for zip code in the Netherlands:
NNNN LL, NNNNLL
The only problem this would give is if there are zipcodes with the , symbol.
My proposition:
/**
* Replace letters of zip code format And check this format on the zip code.
*
* @param string $zipCode zip code
*
* @return bool Indicates whether the zip code is correct
*/
public function checkZipCode($zipCode)
{
if (empty($this->zip_code_format)) {
return true;
}
//explode the different
$zipCodeFormats = explode(',', $this->zip_code_format);
$match = false;
foreach ($zipCodeFormats as $zipCodeFormat) {
$zipRegexp = '/^' . $zipCodeFormat . '$/ui';
$zipRegexp = str_replace(' ', '( |)', $zipRegexp);
$zipRegexp = str_replace('-', '(-|)', $zipRegexp);
$zipRegexp = str_replace('N', '[0-9]', $zipRegexp);
$zipRegexp = str_replace('L', '[a-zA-Z]', $zipRegexp);
$zipRegexp = str_replace('C', $this->iso_code, $zipRegexp);
if ($match = preg_match($zipRegexp, $zipCode)) {
break;
}
}
return (bool) $match;
}
I didn't test this code yet. And we should adapt also the BO in order to explain the admin that he can now allow multiple zip codes formats.
Any updates about this?
I also think this should be improved. In Czech republic, some people will enter code as NNNNN and others as NNN NN. The error that shows is not so clear to all users (they don't understand why space would matter).
Can this be reopend?
Same issue here. I just opened a shop in the Netherlands, and the first feedback I got was about the space.
One fix is to add multiple formats, possibly comma-seperated.
Another possible fix is to introduce seperate characters to indicate if another character is optional.
A third possible fix is to have characters for optional-space and optional-hyphen besides mandatory-space and mandatory-hyphen.