In this article, we will cover how to add Magento 2 validation in System Configuration.
Magento 2 Validation in Configuration
Here is a simple example after creating a file system.xml.
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="bss" translate="label" sortOrder="300">
<label><![CDATA[Bss Commerce]]></label>
</tab>
<section id="bss_admin_config_validate" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Admin Config Validate</label>
<tab>bss</tab>
<resource>Bss_AdminConfigValidate:config</resource>
<group id="bss_product_image_by_customer_general" translate="label" type="text" sortOrder="90" 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>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[Version 1.0.0]]></comment>
</field>
<field id="speed_slide" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Slideshow Speed</label>
</field>
</group>
</textsection>
</system>
</config>
Go to Stores -> Settings -> Configuration -> Bss Commerce -> Admin Config Validate, we get a page admin config with fields already:
Also, NEED TO CHECK NOW >>> Magento Health Check to enhance the overall performance and security.
In which:
- Enable: Select “Yes” to enable the module, “No” to disable the module.
- Slideshow Speed: Fill in numbers.
Furthermore, Magento 2 provides the available classes to help you ask Users to fill in required Magento 2 field types through the class defined in the tag with the structure: [name-class-validate].
Please note that there are various Magento 2 field types in a tag by calling classes separating by “a space”:
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="bss" translate="label" sortOrder="300">
<label><![CDATA[Bss Commerce]]></label>
</tab>
<section id="bss_admin_config_validate" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Admin Config Validate</label>
<tab>bss</tab>
<resource>Bss_AdminConfigValidate:config</resource>
<group id="bss_product_image_by_customer_general" translate="label" type="text" sortOrder="90" 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>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATspeedA[Version 1.0.0]]></comment>
</field>
<field id="speed_slide" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Slideshow Speed</label>
<validate>validate-not-negative-number</validate>
</field>
</group>
</section>
</system>
</config>
After adding Magento 2 validation tag: <validate> validate-not-negative-number </ validate>, a message “Please enter number 0 or greater than in this field” is shown as below image.
Otherwise, Magento also supports you validate server side through by:
<backend_model>[Name_Company]\[Name_module]\Model\Config\Backend\[Name_Class]
In the tag backend_model, we declare a class which is inherited from parent class \Magento\Framework\App\Config\Value. The parent class supports functions such as beforeSave, afterSave, getData.
Here is a simple example of function “saveBefore”.
public function beforeSave()
{
if ($this->getValue() == '') {
throw new \Magento\Framework\Exception\ValidatorException(__($label . ' is required.'));
} else if (!is_numeric($this->getValue())) {
throw new \Magento\Framework\Exception\ValidatorException(__($label . ' is not a number.'));
}
$this->setValue(intval($this->getValue()));
parent::beforeSave();
}
Some Important Magento 2 Validation Classes
Here come several validation classes: Magento 2 form validation, Magento 2 zip code validation, Magento yes no source model, etc.
validate-no-html-tags | HTML tags are not allowed. |
validate-select | Please select an option. |
validate-no-empty | Empty Value |
validate-alphanum-with-spaces | Please use only letters (a-z or A-Z), numbers (0-9) or spaces only in this field. |
validate-data | Please use only letters (a-z or A-Z), numbers (0-9) or underscore (_) in this field, and the first character should be a letter. |
validate-street | Please use only letters (a-z or A-Z), numbers (0-9), spaces and “#” in this field. |
validate-phoneStrict | Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890. |
validate-phoneLax | Please enter a valid phone number. For example (123) 456-7890 or 123-456-7890. |
validate-fax | Please enter a valid fax number (Ex: 123-456-7890). |
validate-email | Please enter a valid email address (Ex: johndoe@domain.com). |
validate-emailSender | Please enter a valid email address (Ex: johndoe@domain.com). |
validate-password | Please enter 6 or more characters. Leading and trailing spaces will be ignored. |
validate-admin-password | Please enter 7 or more characters, using both numeric and alphabetic. |
validate-url | Please enter a valid URL. Protocol is required (http://, https:// or ftp://). |
validate-clean-url | Please enter a valid URL. For example http://www.example.com or www.example.com. |
validate-xml-identifier | Please enter a valid XML-identifier (Ex: something_1, block5, id-4). |
validate-ssn | Please enter a valid social security number (Ex: 123-45-6789). |
validate-zip-us | Please enter a valid zip code (Ex: 90602 or 90602-1234). |
validate-date-au | Please use this date format: dd/mm/yyyy. For example 17/03/2006 for the 17th of March, 2006. |
validate-currency-dollar | Please enter a valid $ amount. For example $100.00. |
If you have any problems or need our professional service for Magento 2 validation, Magento 2 form validation, Magento 2 zip code validation, or Magento yes no source model, etc. please email us support@bsscommerce.com. You can also find the right solution in our best-in-class Magento 2 extension.
About BSS Commerce:
We are one of the leading Magento extension providers and web development services in the world. With experienced and certified Magento developers, we commit to bring high-quality products and services to optimize our business effectively. Let us know about your problems. We are willing to support you every time.