Site icon MageComp Blog

Magento 2: How to Add Custom Validation To System Configuration Field

How to Add Custom Validation To System Configuration Field in magneto 2

Hello Magento Friends ?,

What’s going on? Welcome to Magento 2 technical solutions by MageComp. Today I am here demonstrating How to Add Custom Validation To System Configuration Field in Magento 2. Earlier I explained about How to Remove Apply Discount Code From Checkout Payment Page in Magento 2.

Beginning:

Validation is to restrict data entry on specific fields to avoid false entry. If the data does not match the validation rules it provides an error message telling the user to enter the correct data and do not allow the user to proceed further.

By default, Magento 2 provides some default built-in classes to validate system configuration fields. E.g. required field, validate non-negative numbers, a number greater than zero.

But what if you want to validate your system configuration field based on your requirement? E.g. validate the mobile number field with a minimum 10 digit number.

No worries, with the help of the below code you can Add Custom Validation in Magento 2 System Configuration Field.

So let us find out ?

Steps to Add Custom Validation To System Configuration Field in Magento 2:

Step 1: Update your system.xml file inside the following path: 

app\code\Vendor\Extension\etc\adminhtml\

Now, update the code with the below code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/system_file.xsd">
    <system>
        <section id="customsection" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Custom Validation</label>
            <resource>Vendor_Extension::custom_config</resource>
            <group id="general" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="mobilenumber" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Mobile Number</label>
                    <validate>required-entry validate-ten-degit</validate>
                </field>
            </group>
        </section>
    </system>
</config>

Note: Here “validate-ten-digit” is a Custom validation class.

Step 2: Next, create a requirejs-config.js file inside the below path:

app\code\Vendor\Extension\view\adminhtml\

And add this code:

var config = {
    config: {
        mixins: {
            'mage/validation': {
                'Vendor_Extension/js/admin-config/validator-rules-mixin': true
            }
        }
    }
};

Step 3: Next, create a validator-rules-mixin.js file inside the below path: 

app\code\Vendor\Extension\view\adminhtml\web\js\admin-config\

And add this code:

define([
 'jquery'
], function ($) {
    'use strict';
    return function (target) {
        $.validator.addMethod(
            'validate-ten-digit',
            function (value) {
                return !(value.length < 10);
            },
            $.mage.__('Please enter a 10 digit number.')
        );
        return target;
    };
});

Step 4: Finally run below commands.

php bin/magento setup:upgrade
 php bin/magento setup:static-content:deploy -f 
 php bin/magento cache:flush

That’s it.

Wrap Up:

I hope now you found the way to Add Custom Validation to System Configuration Field in Magento 2. If you find it hard to get, mention right away in the comment section below. I will be glad to fix up. Share the article with your friends and stay in touch with us.

Happy Coding ?

Exit mobile version