0

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 @Lounik – Prathap Gunasekaran Apr 2 at 11:54
  • hi, thank you. I tried with region_id but unfortunately it doesn't work as well – Lounik Apr 3 at 9:37
  • try it vendor file to verify it then make it in your file @Lounik – Prathap Gunasekaran Apr 3 at 9:52
0

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 Apr 3 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.