{"id":627,"date":"2019-10-04T11:04:52","date_gmt":"2019-10-04T11:04:52","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/10\/04\/add-validation-in-system-configuration-in-magento-2\/"},"modified":"2025-05-22T15:46:08","modified_gmt":"2025-05-22T10:16:08","slug":"add-validation-in-system-configuration-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-validation-in-system-configuration-in-magento-2\/","title":{"rendered":"How to Add Validation in System Configuration in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 is a flexible platform that allows extending its functionality to satisfy the business requirements. Data validation is one such integral part that needs to be implemented for any type of E-commerce stores.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Not only frontend, but you may also want to <strong>add validation in system configuration in Magento 2<\/strong>. For example, validate the Email, check for the blank field, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using the below solution, you can validate any type of input fields in the Magento 2 stores configuration such as text field, date, drop-down, etc. as shown in the figure:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/09\/How-to-Add-Validation-in-System-Configuration-in-Magento-2-1-1024x491.png\" alt=\"How to Add Validation in System Configuration in Magento 2\" class=\"wp-image-6517\" style=\"width:840px;height:auto\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Validation in System Configuration in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Place the below code at <em><strong>app\/code\/Meetanshi\/HelloWorld\/etc\/adminhtml\/system.xml<\/strong><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\">\n    &lt;system>\n        &lt;tab id=\"meetanshi\" translate=\"label\" sortOrder=\"50\">\n            &lt;label>&lt;![CDATA[&lt;img src=\"https:\/\/meetanshi.com\/media\/logo.png\" alt=\"Meetanshi\" height=\"20\" style=\"vertical-align:middle;\"\/>]]>&lt;\/label>\n        &lt;\/tab>\n        &lt;section id=\"helloworld\" translate=\"label\" type=\"text\" sortOrder=\"40\" showInDefault=\"1\" showInWebsite=\"1\"\n                 showInStore=\"1\">\n            &lt;class>separator-top&lt;\/class>\n            &lt;label>Hello World&lt;\/label>\n            &lt;tab>meetanshi&lt;\/tab>\n            &lt;resource>Meetanshi_HelloWorld::config_helloworld&lt;\/resource>\n            &lt;group id=\"general\" translate=\"label\" type=\"text\" sortOrder=\"20\" showInDefault=\"1\" showInWebsite=\"1\"\n                   showInStore=\"1\">\n                &lt;label>Configuration&lt;\/label>\n                &lt;field id=\"enable\" translate=\"label comment\" type=\"select\" sortOrder=\"10\" showInDefault=\"1\"\n                       showInWebsite=\"1\" showInStore=\"1\">\n                    &lt;label>Hello World&lt;\/label>\n                    &lt;source_model>Magento\\Config\\Model\\Config\\Source\\Enabledisable&lt;\/source_model>\n                &lt;\/field>\n            &lt;\/group>\n        &lt;\/section>\n    &lt;\/system>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with fellow developers via social media to validate the input fields in Magento 2 system configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 is a flexible platform that allows extending its functionality to satisfy the business requirements. Data validation is one such integral part that needs&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-627","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=627"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/627\/revisions"}],"predecessor-version":[{"id":15244,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/627\/revisions\/15244"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}