{"id":7343,"date":"2019-09-28T18:55:09","date_gmt":"2019-09-28T13:25:09","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7343"},"modified":"2019-10-04T11:22:58","modified_gmt":"2019-10-04T05:52:58","slug":"magento-2-validate-custom-form","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-validate-custom-form\/","title":{"rendered":"Magento 2 &#8211; How To Validate Custom Form"},"content":{"rendered":"<p>In this post I am going to explain how to validate custom form in magento 2.<\/p>\n<p>The data validation is the important step before the submission. The data needs to be checked for correct input and the validity of data can be checked via default Magento 2 validation rules.<\/p>\n<p>Follow the steps to add validation inside your custom form.<\/p>\n<p>Let&#8217;s first create a sample form with some fields. Add the following code inside it.<\/p>\n<pre class=\"lang:default decode:true \">&lt;form class=\"form\" id=\"customForm\" method=\"post\" enctype=\"multipart\/form-data\" autocomplete=\"off\" data-mage-init='{\"validation\":{}}'&gt;\r\n    &lt;?= $block-&gt;getBlockHtml('formkey') ?&gt;\r\n    &lt;fieldset class=\"fieldset\"&gt;\r\n        &lt;h3 class=\"legend\"&gt;\r\n            &lt;span&gt;Contact Information&lt;\/span&gt;\r\n        &lt;\/h3&gt;\r\n        &lt;div class=\"field name required\"&gt;\r\n            &lt;label class=\"label\" for=\"name\"&gt;&lt;span&gt;&lt;?= $block-&gt;escapeHtml(__('Name')) ?&gt;&lt;\/span&gt;&lt;\/label&gt;\r\n            &lt;div class=\"control\"&gt;\r\n                &lt;input name=\"name\" id=\"name\" title=\"&lt;?= $block-&gt;escapeHtmlAttr(__('Name')) ?&gt;\" value=\"\" class=\"input-text\" type=\"text\" data-validate=\"{required:true}\" \/&gt;\r\n            &lt;\/div&gt;\r\n        &lt;\/div&gt;\r\n        &lt;div class=\"field email required\"&gt;\r\n            &lt;label class=\"label\" for=\"email\"&gt;&lt;span&gt;&lt;?= $block-&gt;escapeHtml(__('Email')) ?&gt;&lt;\/span&gt;&lt;\/label&gt;\r\n            &lt;div class=\"control\"&gt;\r\n                &lt;input name=\"email\" id=\"email\" title=\"&lt;?= $block-&gt;escapeHtmlAttr(__('Email')) ?&gt;\" value=\"\" class=\"input-text\" type=\"text\" data-validate=\"{required:true, 'validate-email':true}\" \/&gt;\r\n            &lt;\/div&gt;\r\n        &lt;\/div&gt;\r\n    &lt;\/fieldset&gt;\r\n    &lt;div class=\"actions-toolbar\"&gt;\r\n        &lt;div class=\"primary\"&gt;\r\n            &lt;button type=\"submit\" class=\"action primary\" title=\"&lt;?= $block-&gt;escapeHtml(__('Submit')) ?&gt;\"&gt;\r\n                &lt;span&gt;&lt;?= $block-&gt;escapeHtml(__('Submit')) ?&gt;&lt;\/span&gt;\r\n            &lt;\/button&gt;\r\n        &lt;\/div&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/form&gt;<\/pre>\n<p>Now add following code snippet below the form.<\/p>\n<pre class=\"lang:default decode:true \">&lt;script type=\"text\/x-magento-init\"&gt;\r\n    {\r\n        \"#customForm\": {\r\n            \"validation\": {}\r\n        }\r\n    }\r\n&lt;\/script&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7346\" src=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-form-validation.png\" alt=\"Magento 2 - How To Validate Custom Form\" width=\"744\" height=\"323\" srcset=\"https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-form-validation.png 744w, https:\/\/www.hiddentechies.com\/blog\/wp-content\/uploads\/2019\/09\/custom-form-validation-300x130.png 300w\" sizes=\"(max-width: 744px) 100vw, 744px\" \/><\/p>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to validate custom form in magento 2. The data validation is the important step before the submission. The data needs to be checked for correct input and the validity of data can be checked via default Magento 2 validation rules. Follow the steps to add validation&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-validate-custom-form\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7376,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[9,10,27,696],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7343"}],"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=7343"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7343\/revisions"}],"predecessor-version":[{"id":7347,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7343\/revisions\/7347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7376"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}