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:

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:

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:

Step 4: Finally run below commands.

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 ?

Click to rate this post!
[Total: 10 Average: 2.8]