Hello Magento Friends,

In today’s Magento 2 tutorial, we will learn about How to Apply Custom Product Attribute Validation Programmatically in Magento 2.

While developing an E-commerce store, you receive a lot of data from customers. Adding validation to these data is vital to receive the correct information. In Magento 2, you can create a custom product attribute validation to verify the correct input of the data.

Let’s discover How to Apply Custom Product Attribute Validation Programmatically in Magento 2.

Steps to Apply Custom Product Attribute Validation Programmatically in Magento 2:

Step 1: Create InstallData.php at given below path 

app\code\Vendor\Extension\Setup\InstallData.php

Add the below code

Step 2: Create di.xml at the below path

app\code\Vendor\Extension\etc\di.xml

Now, add the below code

Step 3: Create Validationrules.php at the following path

app\code\Vendor\Extension\Plugin\Product\Validationrules.php

And, add the code as follows

Output:

Validation message when 0 is entered on custom product attribute

validation number

Validation message when input is other than the numeric value on custom product attribute

validation alphabet

Conclusion:

Hence, in this manner, you can successfully Apply Custom Product Attribute Validation Programmatically in Magento 2. Besides this, Magento 2: How to Add Custom Validation Rule in knockout Validation Rules.

If you face any difficulty with the execution of the above code, share the issues with me via the comment box. If you found the solution helpful, hit those 5 stars and share the article amongst others. Stay in the know for more updates.

Happy Coding

Click to rate this post!
[Total: 5 Average: 4.8]