UK GB Post Code Zip Validation Magento 2 Space
-
15-04-2021 - |
Pregunta
I need some help with UK GB Post Code Zip Validation Magento 2 Space
In that I want to force the user to enter spaces so its like this EC1A 1BB or W1A 0AX or M1 1AE so there must be space After the 2 3 or 4 charachter. so far I have spent several hours but not found any solution and only file i found is vendor/magento/module-directory/etc zip_codes.xml
At the moment it is accepting entry like this bt559zq which I dont want
Without this my shipping prices in table rates are not being calculated correctly
Need this setting wherever there is address for example customer accounts . one page checkout, cart shipping estimator
Can someone please help asap
thanks
Solución
You need to create a new module and add etc/zip_codes.xml with the following:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Directory:etc/zip_codes.xsd">
<zip countryCode="GB">
<codes>
<code id="pattern_1" active="true" example="AB12 3CD">^[a-zA-Z]{2}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_2" active="true" example="A1B 2CD">^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_3" active="true" example="AB1 2CD">^[a-zA-Z]{2}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_4" active="true" example="AB1C 2DF">^[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_5" active="true" example="A12 3BC">^[a-zA-Z]{1}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_6" active="true" example="A1 2BC">^[a-zA-Z]{1}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
</codes>
</zip>
</config>
These are pretty the same patterns to vendor/magento/module-directory/etc zip_codes.xml exclude a space is a mandatory now.
Also, you need to add Magento_Directory sequence into etc/module.xml file.
Otros consejos
Create app/code/Jt/Postcodespace/registration.php
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Jt_Postcodespace',
__DIR__
);
Create app/code/Jt/Postcodespace/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Jt_Postcodespace" setup_version="1.0.0">
<sequence>
<module name="Magento_Directory" />
</sequence>
</module>
</config>
Create app/code/Jt/Postcodespace/etc/zip_codes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Directory:etc/zip_codes.xsd">
<zip countryCode="GB">
<codes>
<code id="pattern_1" active="true" example="AB12 3CD">^[a-zA-Z]{2}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_2" active="true" example="A1B 2CD">^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_3" active="true" example="AB1 2CD">^[a-zA-Z]{2}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_4" active="true" example="AB1C 2DF">^[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_5" active="true" example="A12 3BC">^[a-zA-Z]{1}[0-9]{2}\s[0-9]{1}[a-zA-Z]{2}$</code>
<code id="pattern_6" active="true" example="A1 2BC">^[a-zA-Z]{1}[0-9]{1}\s[0-9]{1}[a-zA-Z]{2}$</code>
</codes>
</zip>
</config>
After that run in the app root dir in console:
php bin/magento setup:upgrade