Magento 2: Validation in system configuration

System Configuration Validation Magento 2

Another short and sweet article for our Magento 2 developers to find out what all validations are available in Magento 2 to add to system configuration in adminhtml system.xml-:

‘validate-no-html-tags’ => ‘Validate no HTML tags allowed’
‘validate-select’ => ‘Validate option has been selected or not’
‘required-entry’ => ‘Validate required field’
‘validate-number’ => ‘Validate a valid number in the field’
‘validate-digits’ => ‘Validate only digits in the field for example no dots or no comma’
‘validate-date’ => ‘Validate a valid date’
‘validate-email’ => ‘Validate a valid email address. For example johndoe@domain.com’
‘validate-url’ => ‘Validate a valid URL. Protocol is required (http://, https:// or ftp://)’
‘validate-not-negative-number’ => ‘Validate no negative number in the field’
‘validate-zero-or-greater’ => ‘Validate a number should be either 0 or greater in this field’
‘validate-state’ => ‘Validate valid State/Province should be selected’
‘validate-cc-number’ => ‘Validate a valid credit card number’
‘validate-data’ => ‘Validate this field should only contain letters (a-z or A-Z), numbers (0-9) or underscore(_) in this field’
‘validate-no-empty’ => ‘Validate the field shouldn’t be empty’
‘validate-length’ => ‘Validate the length of the field’
‘minimum-length-10’ => ‘Validate the minimum length of the field’
‘maximum-length-250’ => ‘Validate the maximum length of the field’

Quick example to show how this can be used in Magento 2 in your system.xml configuration

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="scommerce" translate="label" sortOrder="200">
<label>Scommerce Configuration</label>
</tab>
<section id="deliveryinstruction" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Delivery Instruction</label>
<tab>scommerce</tab>
<resource>Scommerce_DeliveryInstruction::configuration</resource>
<group id="configuration" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Configuration</label>
<field id="label" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Delivery Instruction Label</label>
<comment>Delivery instruction label</comment>
<validate>required-entry validate-length minimum-length-10 maximum-length-32</validate>
<depends>
<field id="deliveryinstruction/general/enable">1</field>
</depends>
</field>

N.B – With the above code, the delivery instruction label will be validated against mandatory data and the length of the field should be minimum 10 characters but shouldn’t exceed more than 32 characters.

That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Discover more from WHO WILL CARE eCommerce

Subscribe now to keep reading and get access to the full archive.

Continue reading