following this post (Magento 2.2 Input field validation on Checkout page)
I can successfully add validation on the company field. But when I apply that to the region field, it doesn't work anymore. By "doesnt work" I mean that I get no validation message at all, I can write whatever I want in the region field and I get no red message.
What I need is to validate the region field so that the user can write only 2 characters. How can I do that?
What I did:
1. created checkout_index_index.xml in my theme under app/design/frontend/Theme/default/Magento_Checkout/layout
2. Add the code specified in the post mentioned above
3. substitute "company" with "region"
Final xml:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="checkout" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.root">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="checkout" xsi:type="array">
<item name="children" xsi:type="array">
<item name="steps" xsi:type="array">
<item name="children" xsi:type="array">
<item name="shipping-step" xsi:type="array">
<item name="children" xsi:type="array">
<item name="shippingAddress" xsi:type="array">
<item name="children" xsi:type="array">
<!-- To affect the shipping address fields -->
<item name="shipping-address-fieldset" xsi:type="array">
<item name="children" xsi:type="array">
<item name="region" xsi:type="array">
<item name="validation" xsi:type="array">
<item name="min_text_length" xsi:type="number">0</item>
<item name="max_text_length" xsi:type="number">9</item>
<item name="letters-only" xsi:type="boolean">true</item>
<item name="required-entry" xsi:type="boolean">true</item>
<item name="max-words" xsi:type="number">2</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
As I said, If I change "region" with "company", it works perfectly. But I need to apply the validation to the region field. Any idea how to achieve this?
Thanks
-
Instead of region give as region_id not sure but guessing that would work out @LounikPrathap Gunasekaran– Prathap Gunasekaran2019年04月02日 11:54:32 +00:00Commented Apr 2, 2019 at 11:54
-
hi, thank you. I tried with region_id but unfortunately it doesn't work as wellLounik– Lounik2019年04月03日 09:37:36 +00:00Commented Apr 3, 2019 at 9:37
-
try it vendor file to verify it then make it in your file @LounikPrathap Gunasekaran– Prathap Gunasekaran2019年04月03日 09:52:36 +00:00Commented Apr 3, 2019 at 9:52
1 Answer 1
Please go to admin
Store->Configuration->General->State Options(Select Your Region. You Want to required).
After Cache Flush.
-
I don't want it to be required. It's ok for it to be optional. What I want is that - if the customer fills it in - he can only write two letters.Lounik– Lounik2019年04月03日 09:36:47 +00:00Commented Apr 3, 2019 at 9:36