In this article, you going to see why the Magento2 form validation not working and what to check for validating a new form in Magento 2.
Let’s start with the point of regarding, Magento 2 check if form is valid.
Before going start to one important thing always remember regarding form validation.
The import point is Magento 2 validate form key.
Please add form-key inside the form like below form this you come to understand completely.
<?= $block->getBlockHtml('formkey') ?>
For this we have to use Magento 2 a form key validate class.
It is defined as below.
Magento\Framework\Data\Form\FormKey\Validator
From below path you come to know that how and where Magento checks form key.
For that you can check it from following Magento core file.
vendor/magento/framework/Data/Form/FormKey/Validator.php
Steps below are regarding the submitation to your local for Magento2 form validation.
For developing any store in Magento 2 platform, file validation always used.
It is used for inject the validation to any file in Magento 2.
There are many different approach to validate a new form in Magento 2 using file called validation
From below declaration of form you come to understand regarding validation.
<form class="form" id="custom-form" method="post" autocomplete="off">
<fieldset class="fieldset">
<legend class="legend"><span><?php echo __('Personal Detail') ?></span></legend><br>
<div class="field required">
<label for="email_address" class="label"><span><?php echo __('Email') ?></span></label>
<div class="control">
<input type="email" name="email" id="email_address" value="" title="<?php echo __('Email') ?>" class="input-text" data-validate="{required:true, 'validate-email':true}">
</div>
</div>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit" class="action submit primary" title="<?php echo __('Submit') ?>"><span><?php echo __('Submit') ?></span></button>
</div>
</div>
</form>
The magento 2 backend form validation process is similar.
The attribute for validation as the form is created.
For accepting this javascript, please execute below script for this as given below.
<script type="text/x-magento-init">
{
"#custom-form": {
"validation": {}
}
}
</script>
You can also directly execute that operation in html
<form data-mage-init='{"validation": {}}' class="form" id="custom-form" method="post" autocomplete="off">
<script type="text/javascript">
require([
'jquery',
'mage/mage'
], function($){
var dataForm = $('#custom-form');
dataForm.mage('validation', {});
});
</script>
After all this point the output of the javascript library is.
lib/web/mage/validation.js
We can achieve magento 2 form validation without submit using above declaration of code or using ajax also.
Finally done with Magento 2 form validation all points.
During validation of form you can check with controller regarding how the controller is call for validation process
I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.