Hello Everyone,
In this blog, we will learn about how to Add Custom Validation Rule in Configuration file system.xml in Magento 2.
Magento 2 provides lots of default validation rules, but sometimes we need to add our custom validation like validate a mobile number field with a minimum 10 digit number.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Custom Validation Rule in Configuration File in Magento 2.
STEPS FOR ADD CUSTOM VALIDATION RULE IN CONFIGURATION FILE IN MAGENTO 2
Step 1: Update system.xml file
app/code/Vendor/Extension/etc/adminhtml/system.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="magecurious" class="magecurious" translate="label" sortOrder="10">
<label>Magecurious</label>
</tab>
<section id="extension" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Magecurious</label>
<tab>magecurious</tab>
<resource>Vendor_Extension::config_extension</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Configuration</label>
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Extension</label>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
<field id="mobileno" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Mobile Number</label>
<validate>required-entry validate-ten-digit</validate>
</field>
</group>
</section>
</system>
</config>
In above code we used <validate>required-entry validate-ten-digit</validate> which is our new validation rule.
Here “validate-ten-digit” is a Custom validation class.
Step 2: Create requirejs-config.js file
app/code/Vendor/Extension/view/adminhtml/requirejs-config.js
var config = {
config: {
mixins: {
'mage/validation': {
'Vendor_Extension/js/admin-config/validator-rules-mixin': true
}
}
}
};
Step 3: Create validator-rules-mixin.js file
app/code/Vendor/Extension/view/adminhtml/web/js/admin-config/validator-rules-mixin.js
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 the below commands
$ php bin/magento setup:upgrade
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 5: Output:
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Add Custom Validation in the system.xml configuration file in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.
