Is it possible to call the active form validation programmatically via JavaScript? I need to call the validation procedure before doing some ajax operations.
-
You can find a documentation here : https://yii2-cookbook-test.readthedocs.io/forms-activeform-js/ – Timothée Planchais Mar 22 '21 at 00:59
9 Answers
Guess I'm a bit late with a reply here but I just had the same question and the solution by soju did not work for me either.
So I looked a bit deeper into the JS-code of ActiveForm and found that it appears to monitor the status of each field in a variable and if the field is "untouched" the validation isn't triggered, unless submitting the actual form. So I changed my call to this:
var $form = $("#my-form"),
data = $form.data("yiiActiveForm");
$.each(data.attributes, function() {
this.status = 3;
});
$form.yiiActiveForm("validate");
This now appears to be working as I expect.

- 849
- 1
- 9
- 25
-
3I am not sure why this is not accepted answer since it is the first thing it worked like expected. – Edna Feb 12 '17 at 00:53
-
`validate` takes a parameter called `forceValidate`. In short ... all you need to do is: `$('#my-form').yiiActiveForm('validate', true);` – Definitely not Rafal Aug 12 '22 at 06:53
We can achieve that by merging @BlueZed and @S Rana's answer.
You can write below script, so we can check that if form has any error in it then form will not submit (Even It will work for tabular (wizards) like form ).
var $form = $("#form"),
data = $form.data("yiiActiveForm");
$.each(data.attributes, function() {
this.status = 3;
});
$form.yiiActiveForm("validate");
if ($("#form").find(".has-error").length) {
return false;
}

- 2,995
- 4
- 52
- 102
-
The example above applies to BS 3. For bootstrap 4 use different CSS error class: `if ($form.find(".is-invalid:first").length) {return false}`. – lubosdz Oct 20 '19 at 19:44
Thanks blue zed, but before this - to append form field, u need to do this stuff...
// your input
$inputData = $form->field($model,"prductName");
// this remove next line & convert double quotes to single quotes
$newInputData= trim(preg_replace('/\s+/', ' ',str_replace('"',"'", $inputData)));
// then append like this
$("#table").append("'.$newInputData.'");
// this worked for me along with blue zend solution like this below
$this->registerJs('
$(document).on("click","input[type=text]",function(){
var $form = $("#w0"),
data = $form.data("yiiActiveForm");
$.each(data.attributes, function() {
this.status = 3;
});
$form.yiiActiveForm("validate");
});
');

- 143
- 6
Yes it is possible, you should try this :
$('#form-id').yiiActiveForm('validate');

- 25,111
- 3
- 68
- 70
-
Hi, thank you for reply. I've tried the above code but I still can't execute the form validation. No javascript errors are returned and if I try to alert the result it gave me "undefiend". – AleCat83 Feb 20 '15 at 09:43
-
This method validate your form inputs and display corresponding errors, it will not return anything... – soju Feb 20 '15 at 14:21
-
1May I need to load some other configurations or assets to make it work? I've tried to run the above code (with my form id) but the validation don't get executed.. – AleCat83 Feb 20 '15 at 15:25
-
This doesn't work also for me. Reason is that in order to perform validation, form must be in submitting state, or inputs must have status 2 or 3, meaning input is in is pending validation. You can force submitting state with passing true as second argument, but this will cause form to submit after validation is done. – Edna Feb 12 '17 at 00:50
this is worked for me
$this->registerJs( "
$('body').on('beforeSubmit', 'form#product-form', function () {
var form = $(this);
// return false if form still have some validation errors
if (form.find('.has-error').length) {
return false;
}
// submit form
$.ajax({
url: form.attr('action'),
type: 'post',
data: form.serialize(),
success: function (response) {
}
});
return false;
}); ");

- 1,215
- 15
- 34
The validation is activated when submitting the form. Thus, this will work:
$form.data('yiiActiveForm').submitting = true;
$form.yiiActiveForm('validate', false);
The second argument to validate
is called forceValidate
.

- 1,768
- 3
- 16
- 29
To validate manually from javascript, you should place following code to end of form submit event.
return $('#your-form-id').yiiActiveForm('validate');
For novice users,
$('#your-form-id').submit(function () {
return $('#your-form-id').yiiActiveForm('validate');
});
Also note that you should specify form id on ActiveForm
like below
<?php $form = ActiveForm::begin(['id' => 'd4d-hub-form']); ?>

- 1,365
- 20
- 27
I had the same issue. And the are no clearly solution in official documentation and I don't know why any solution on Stackoverflow does not work for me. May be in the different versions of yii2 there is different ways to do that. I have spent a lot of time to find solution. In my case I have triggered validation for individual input on the form:
$('#form-id').data('yiiActiveForm').submitting = false;
$('#form-id').yiiActiveForm('validateAttribute', 'input-id'); //this will triger validation for input with id 'input-id'
$('#form-id').yiiActiveForm('validateAttribute', 'other-input-id'); //this will triger validation for input with id 'other-input-id'
please notice second parameter of function yiiActiveForm() it is not selector and it is not name of attribute it is id of input of attribute!!!

- 1,614
- 15
- 19
Try this
$("#form-id").data('yiiActiveForm').submitting = true;
$("#form-id").yiiActiveForm('validate');
it will show validation error if any field is not valid. Also if all fields are validate then it will submit the request

- 210
- 3
- 10