Magento 2 – How to Extend Magento_Ui/js/lib/validation/rules.js

magento2

I need to add custom validation rule to postcode field in my checkout page. Therefore I need to extend Magento_Ui/js/lib/validation/rules.js file.

How can I achieve it in custom module (not theme)?

Best Answer

You can override inside your module using below way, app/code/Krish/Checkout/view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
            "Magento_Ui/js/lib/validation/rules": "Krish_Checkout/js/lib/validation/rules"
        }
    }
};

Create js file at app/code/Krish/Checkout/view/frontend/web/js/lib/validation/rules.js

Run php bin/magento setup:static-content:deploy command.

Related Topic