{"id":11897,"date":"2020-12-26T15:06:56","date_gmt":"2020-12-26T09:36:56","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=11897"},"modified":"2021-01-15T16:27:20","modified_gmt":"2021-01-15T10:57:20","slug":"add-validation-system-configuration","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento\/add-validation-system-configuration\/","title":{"rendered":"Magento 2 &#8211; How to Add Validation in System Configuration"},"content":{"rendered":"<p>In this post I am going to explain how to add validation in system configuration in Magento 2.<\/p>\n<p>In order to do validatetion in system configuration field, you just need to add &#8220;validate&#8221; tag for field in system.xml file and pass validation class in it.<\/p>\n<p>See the below example of required field validation and email validation.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\"&gt;\r\n    &lt;system&gt;\r\n        &lt;tab id=\"ht\" translate=\"label\" sortOrder=\"10\"&gt;\r\n            &lt;label&gt;HiddenTechies&lt;\/label&gt;\r\n        &lt;\/tab&gt;\r\n        &lt;section id=\"store_info\" translate=\"label\" type=\"text\" sortOrder=\"5\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n            &lt;label&gt;Test Configuration&lt;\/label&gt;\r\n            &lt;tab&gt;webkul&lt;\/tab&gt;\r\n            &lt;resource&gt;Ht_Mymodule::configuration&lt;\/resource&gt;\r\n            &lt;group id=\"general\" translate=\"label\" type=\"text\" sortOrder=\"5\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\"&gt;\r\n\t\t&lt;field id=\"store_name\" translate=\"label comment\" sortOrder=\"5\" type=\"text\" showInDefault=\"1\" showInWebsite=\"0\" showInStore=\"0\"&gt;\r\n                    &lt;label&gt;Store Name&lt;\/label&gt;\r\n                    &lt;validate&gt;required-entry&lt;\/validate&gt;\r\n                &lt;\/field&gt;                \r\n\t\t&lt;field id=\"store_email\" translate=\"label comment\" sortOrder=\"10\" type=\"text\" showInDefault=\"1\" showInWebsite=\"0\" showInStore=\"0\"&gt;\r\n                    &lt;label&gt;Store Email&lt;\/label&gt;\r\n                    &lt;validate&gt;validate-email&lt;\/validate&gt;\r\n                &lt;\/field&gt;\r\n            &lt;\/group&gt;\r\n        &lt;\/section&gt;\r\n    &lt;\/system&gt;\r\n&lt;\/config&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>One field can have one or more validator-classes. You just need to add space between classes.<\/p>\n<p><strong>&lt;validate&gt;required-entry validate-email&lt;\/validate&gt;<\/strong><\/p>\n<p>The following validation rules are available:<\/p>\n<p>validate-no-html-tags -&gt; HTML tags are not allowed.<br \/>\nvalidate-select -&gt; Select an option.<br \/>\nvalidate-no-empty -&gt; Empty Value<br \/>\nvalidate-alphanum-with-spaces -&gt; Use letters (a-z or A-Z), numbers (0-9), or spaces only in this field.<br \/>\nvalidate-data -&gt; Use letters (a-z or A-Z), numbers (0-9), or underscores (_) only in this field. The first character should be a letter.<br \/>\nvalidate-street -&gt; Use letters (a-z or A-Z), numbers (0-9), spaces, and \u201c#\u201d only in this field.<br \/>\nvalidate-phoneStrict -&gt; Enter a valid phone number. For example, (123) 456-7890 or 123-456-7890.<br \/>\nvalidate-phoneLax -&gt; Enter a valid phone number. For example, (123) 456-7890 or 123-456-7890.<br \/>\nvalidate-fax -&gt; Enter a valid fax number. For example, 123-456-7890.<br \/>\nvalidate-email -&gt; Enter a valid email address. For example, johndoe@domain.com.<br \/>\nvalidate-emailSender -&gt; Enter a valid email address. For example, johndoe@domain.com.<br \/>\nvalidate-password -&gt; Enter 6 or more characters. Leading and trailing spaces will be ignored.<br \/>\nvalidate-admin-password -&gt; Enter 7 or more characters, using both numeric and alphabetic.<br \/>\nvalidate-url -&gt; Enter a valid URL. Protocol is required (http:\/\/, https:\/\/ or ftp:\/\/).<br \/>\nvalidate-clean-url -&gt; Enter a valid URL. For example, http:\/\/www.example.com or www.example.com.<br \/>\nvalidate-xml-identifier -&gt; Enter a valid XML-identifier. For example, something_1, block5, id-4.<br \/>\nvalidate-ssn -&gt; Enter a valid social security number. For example, 123-45-6789.<br \/>\nvalidate-zip-us -&gt; Enter a valid ZIP code. For example, 90602 or 90602-1234.<br \/>\nvalidate-date-au -&gt; Use this date format: dd\/mm\/yyyy. For example, 17\/03\/2006 for the 17th of March, 2006.<br \/>\nvalidate-currency-dollar -&gt; Enter a valid $ amount. For example, $100.00.<\/p>\n<div class=\"angwp_12010 _ning_cont _ning_hidden _ning_outer _align_center responsive\" data-size=\"custom\" data-bid=\"12010\" data-aid=\"0\" style=\"max-width:800px; width:100%;height:inherit;\"><div class=\"_ning_label _left\" style=\"\"><\/div><div class=\"_ning_inner\" style=\"\"><a href=\"https:\/\/www.hiddentechies.com\/blog?_dnlink=12010&t=1778959840\" class=\"strack_cli _ning_link\" target=\"_blank\">&nbsp;<\/a><div class=\"_ning_elmt\"><img decoding=\"async\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/angwp\/items\/12010\/Banner-2.png\" \/><\/div><\/div><\/div><div class=\"clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to add validation in system configuration in Magento 2. In order to do validatetion in system configuration field, you just need to add &#8220;validate&#8221; tag for field in system.xml file and pass validation class in it. See the below example of required field validation and email&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento\/add-validation-system-configuration\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":12263,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[181,59,2138],"tags":[729,9,10,2153],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11897"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=11897"}],"version-history":[{"count":3,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11897\/revisions"}],"predecessor-version":[{"id":12262,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/11897\/revisions\/12262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/12263"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=11897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=11897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=11897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}