Magento 2 – Fix Validation Not Working

checkoutmagento2validation

I am looking for validate the city field in checkout page. The validation rules are city name must be chennai.

I follow this

I have add below lines in <magento-root>/vendor/magento/module-ui/view/base/web/js/lib/validation/rules.js :

"chennai_text_value": [
        function (value, params) {
            return value != params;
        },
        $.mage.__('This product only applicable for chennai people')
 ],

I have add below lines in <magento-root>/vendor/magento/module-checkout/view/frontend/layout/checkout_index_index.xml after company field validation(name="company" find in layout for easy understand)

<item name="city" xsi:type="array">
    <item name="validation" xsi:type="array">
        <item name="chennai_text_value" xsi:type="string">chennai</item>
    </item>
</item>

but nothing can happen, I expect chennai only accept in city field.

any help?

Best Answer

problem got solved after changing condition in rules.js (value == params)

"chennai_text_value": [
    function (value, params) {
        return value == params;
    },
    $.mage.__('This product only applicable for chennai people')
 ],

note: its not a good practice to change magento core file

Related Topic