How to validate yii2 activeform attribute using jquery ?
How to validate yii2 activeform attribute using jquery ?
You can do it this way:
$("#form").yiiActiveForm("validate");
But I only need to validate 1 attribute exactly
You can manually trigger the validation of the attribute with the specified ID using validateAttribute method. Try this:
jQuery("[your-form-selector]").yiiActiveForm("validateAttribute", "[your-field-id]");
or alternatively:
jQuery("[your-form-selector]").yiiActiveForm("find", "[your-field-id]").validate();
Uncaught Error: Method validateAttribute does not exist on jQuery.yiiActiveForm
It seems that my activeform.js doesn’t have validateattribute method http://pastebin.com/ntmwAQKN
About using alternative:
I always get Uncaught TypeError: Cannot read property ‘attributes’ of undefined, on this line:
var attributes = $(this).data('yiiActiveForm').attributes,
This is probably because of wrong field id. Paste your code so I could help you to find solution.
jQuery("[your-form-selector]").yiiActiveForm("validateAttribute", "[your-field-id]");
It’s fine.
But I also need to know if validation result is good or bad. Maybe validate() returns something, but after trying it:
jQuery("[your-form-selector]").yiiActiveForm("find", "[your-field-id]").validate();
it gives me this error in the chrome console:
Uncaught TypeError: Cannot read property 'push' of undefined
yii.validation.pub.addMessage @ yii.validation.js:20
yii.validation.pub.required @ yii.validation.js:35
jQuery.yiiActiveForm.validate @ index.php?r=site/submit-test-form:2669
(anonymous function) @ test-form.js:9
jQuery.extend.each @ jquery.js:374
jQuery.fn.jQuery.each @ jquery.js:139
changeView @ test-form.js:6
onclick @ index.php?r=site/submit-test-form:1183
I had this issue on a form and I couldn’t get it to validate only one field. If you are in a form then you can use a work around to make it fire.
$("#yourFieldID").blur();
That will tell yii to fire the validation. I couldn’t get calling the validation directly to work.
This works for me (Yii 2.0.6):
$('#formId').yiiActiveForm('validateAttribute', 'input-id')
In my case it was a $(document).ready ordering issue.
Notice that the scripts are loaded before the page javascript that instantiate the the form’s validation.
What I had to do was change the order of script calls.
What I had to do was change the order of script calls.
How did you do that?
I can’t load my asset after validation js file, so it is always calls in wrong order.