{"id":23977,"date":"2025-11-05T11:00:00","date_gmt":"2025-11-05T05:30:00","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=23977"},"modified":"2025-11-06T10:13:33","modified_gmt":"2025-11-06T04:43:33","slug":"how-add-javascript-form-validation-hyva-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/how-add-javascript-form-validation-hyva-magento-2\/","title":{"rendered":"How to Add JavaScript Form Validation in Hyv\u00e4 Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Hyv\u00e4 themes primarily use HTML 5 browser validation. However, there are times when you need more control like displaying custom error messages or implementing complex validation logic.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hyv\u00e4 provides a powerful and flexible JavaScript form validation library built on Alpine.js. This guide provides a step-by-step to add JavaScript form validation in Hyv\u00e4 Magento 2 website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step-by-Step Tutorial: JavaScript Form Validation in Hyv\u00e4 Theme<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can prepare your validation form using these three steps in your Hyv\u00e4:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. <\/strong>Activate the JS Validation Library<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First load the required JavaScript on the relevant pages to integrate form validation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can be achieved by applying the <strong>hyva_form_validation<\/strong> layout handle within your layout XML file.<\/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=\"\">XML\n\n&lt;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n\u00a0 \u00a0 &lt;body>\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;referenceContainer name=\"content\">\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;block name=\"my-custom-form\" template=\"My_Module::form.phtml\">\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;arguments>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;argument name=\"hyva_form_validation\" xsi:type=\"boolean\">true&lt;\/argument>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;\/arguments>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;\/block>\n\u00a0 \u00a0 \u00a0 \u00a0 &lt;\/referenceContainer>\n\u00a0 \u00a0 &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Initialize the Alpine.js Component<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Utilize <a href=\"http:\/\/alpine.js\" target=\"_blank\" rel=\"noopener\">Alpine.js<\/a> to initialize the validation component for your form element. Make sure your basic HTML form contains the below code.<\/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=\"\">apply x-data=\"hyva.formValidation($el)\".\n\nHTML\n\n&lt;form x-data=\"hyva.formValidation($el)\" @submit=\"onSubmit\">\n\u00a0 \u00a0 &lt;\/form><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><br>If you require adding custom logic then you will need to integrate the validation component into your Alpine component.<\/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=\"\">HTML\n\n&lt;form x-data=\"{...hyva.formValidation($el), myCustomProperty: 'value'}\" @submit=\"onSubmit\">\n\u00a0 \u00a0 &lt;\/form><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Trigger Validation Events<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The library offers a streamlined approach to form validation with Alpine.js events integration. Allowing developers to leverage the event-driven architecture of Alpine.js for handling form submissions and validating user input.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a form is submitted, or when specific input fields are interacted with, the library hooks into standard Alpine.js events, enabling real-time validation feedback and ensuring data integrity before processing.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Form Submission<\/strong>: To validate all fields upon form submission, use<strong> @submit=&#8221;onSubmit&#8221; <\/strong>on the &lt;form> tag. The library automatically adds the novalidate attribute to prevent browser validation conflicts.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Individual Fields<\/strong>: For real-time validation as a user interacts with a field, apply an event like <strong>@change=&#8221;onChange&#8221;<\/strong> directly to the &lt;input> element.<\/li>\n<\/ul>\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=\"\">HTML\n\n&lt;div class=\"field field-reserved\">\n\u00a0 \u00a0 &lt;label for=\"email\">Email&lt;\/label>\n\u00a0 \u00a0 &lt;input type=\"email\" name=\"email\" id=\"email\"\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 @change=\"onChange\"\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 required>\n&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s recommended to use the<strong> field-reserved classes<\/strong> on the wrapper div to avoid layout shifts when error messages appear.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Defining Validation Rules<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can define validation rules in two ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>HTML5 Attributes<\/strong>: The library automatically picks up native browser constraints like required, minlength, maxlength, pattern, min, max, and step.<\/li>\n\n\n\n<li><strong>data-validate Attribute<\/strong>: For custom rules or multiple rules, use the data-validate attribute.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Important:<\/strong> The value must be a valid JSON object (keys and string values in double quotes).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example combining both methods:<\/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=\"\">HTML\n\n&lt;div class=\"field field-reserved\">\n\u00a0 \u00a0 &lt;label for=\"username\">Username&lt;\/label>\n\u00a0 \u00a0 &lt;input type=\"text\" id=\"username\" name=\"username\"\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 required\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 data-validate='{\"minlength\": 5, \"maxlength\": 15}'\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 data-msg-minlength=\"Username must be at least %0 characters long.\"\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 class=\"input-text\">\n&lt;\/div><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the data-msg-minlength attribute to customize the default validation message\u00a0<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, it allows to override any validator&#8217;s default message by following the format data-msg-VALIDATOR_NAME.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;The %0 placeholder will automatically be replaced with the rule&#8217;s argument (e.g., &#8220;5&#8221;).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Available Built-in Validators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hyv\u00e4 includes a set of common validators out of the box.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Name<\/strong><\/td><td><strong>Usage Examples<\/strong><\/td><td><strong>Note<\/strong><\/td><\/tr><tr><td>required<\/td><td>required or data-validate='{&#8220;required&#8221;: true}&#8217;<\/td><td>Uses the browser&#8217;s constraint API.<\/td><\/tr><tr><td>minlength<\/td><td>minlength=&#8221;2&#8243; or data-validate='{&#8220;minlength&#8221;: 2}&#8217;<\/td><td>Uses the browser&#8217;s constraint API on text inputs.<\/td><\/tr><tr><td>maxlength<\/td><td>maxlength=&#8221;8&#8243; or data-validate='{&#8220;maxlength&#8221;: 8}&#8217;<\/td><td>Uses the browser&#8217;s constraint API on text inputs.<\/td><\/tr><tr><td>pattern<\/td><td>pattern=&#8221;[A-Z]{5}&#8221; or data-validate='{&#8220;pattern&#8221;: &#8220;[A-Z]{5}&#8221;}&#8217;<\/td><td>Uses the browser&#8217;s constraint API.<\/td><\/tr><tr><td>min<\/td><td>min=&#8221;1&#8243; or data-validate='{&#8220;min&#8221;: 1}&#8217;<\/td><td>For number inputs.<\/td><\/tr><tr><td>max<\/td><td>max=&#8221;10&#8243; or data-validate='{&#8220;max&#8221;: 10}&#8217;<\/td><td>For number inputs.<\/td><\/tr><tr><td>step<\/td><td>step=&#8221;2&#8243; or data-validate='{&#8220;step&#8221;: 2}&#8217;<\/td><td>For number inputs.<\/td><\/tr><tr><td>email<\/td><td>type=&#8221;email&#8221; or data-validate='{&#8220;email&#8221;: true}&#8217;<\/td><td>Uses Magento&#8217;s email validation regex.<\/td><\/tr><tr><td>password<\/td><td>data-validate='{&#8220;password&#8221;: true}&#8217;<\/td><td>Uses Magento&#8217;s password validation regex.<\/td><\/tr><tr><td>equalTo<\/td><td>data-validate='{&#8220;equalTo&#8221;: &#8220;#password&#8221;}&#8217;<\/td><td>Compare the field&#8217;s value to another field&#8217;s value using a selector.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Implementing JavaScript form validation in the Hyv\u00e4 theme not only enhances user experience but also ensures data accuracy and security in your Magento 2 store.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With Hyv\u00e4\u2019s lightweight and performance-focused architecture, you can easily integrate custom validation logic while keeping your site fast and responsive.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whether it\u2019s improving checkout forms or contact forms, proper validation helps prevent errors and builds customer trust.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Start implementing these best practices to make your Hyv\u00e4-based Magento store more reliable and user-friendly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hyv\u00e4 themes primarily use HTML 5 browser validation. However, there are times when you need more control like displaying custom error messages or implementing complex&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5869],"tags":[],"class_list":["post-23977","post","type-post","status-publish","format-standard","hentry","category-hyva-themes"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/23977","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=23977"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/23977\/revisions"}],"predecessor-version":[{"id":24030,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/23977\/revisions\/24030"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=23977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=23977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=23977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}