yii2: $myForm.yiiActiveForm('validate') does NOT run form validation

I have the form:

<?php $form = ActiveForm::begin([
                'enableClientValidation' => false,
                'enableAjaxValidation' => true,
                'validationUrl' => ['validate-form-step-2'],
                'validateOnBlur' => false,
                'validateOnChange' => false
            ]) ?>

This form validates only on button click. Button click handler:

// Getting my form
var $form = $('#<?= $form->id ?>');

// Run validation
$form.yiiActiveForm('validate');

This does nothing. yiiActiveForm has method ‘validate’, nothing happens.

Validation runs only if I call this:

$form.yiiActiveForm('submitForm')

I tried to subscribe on event ‘beforeValidate’ on my form. Event triggered only on call ‘submutForm’.

How to run form validation programmatically? Is it a bug?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

This seems to have not been fixed yet. We need more people of the community pitching in, making pull-requests, and helping the team out. This would make their job a lot easier 👍 I am in the middle of a project, but I will try and make some time to dig deeper into this.

It seems that $("#w0").yiiActiveForm("validate") does not work… at all…???

However, calling $("#w0").yiiActiveForm("validateAttribute", "userprofile-gravatar_email"); directly does.

I also want to point out that @bluezed 's solution to set the status to 3, worked. I tried this first and went OMG it fixed my form validation 😃


My use case:

I have a checkbox for “use gravatar” where the user can enable/disable using a Gravatar profile pic. If disabled, it will use a stock profile pic, unless they upload their own. If enabled, it will override the in-house option and replace it with their Gravatar.

It is possible that the user’s Gravatar account matches a different email address than they registered on my site with. So, I make them enter in the email address. Also, for accounts, the email must be unique, but if they had more than one account, they could use the same Gravatar for all of them.

So I also needed to have them enter the Gravatar email address.

Now, the checkbox for “use_gravatar” is not required, they can check/uncheck it. However, if checked, that then means that the “gravatar_email” text input must be required!


I struggled getting this to work. When I clicked the checkbox, it didn’t pop up red on the email box. It would show the error only after I: clicked into the email box then clicked off of it. So I had to focus, then blur, the email box… when toggling my checkbox… grrr

I messed with whenClient on my models rules… following guides, issues, stack overflow, google (I almost used Yahoo!!)… still not working right. It was off, and clunky…

So then I added registerJS to my view, to a jQuery onChange, prop, keyup… tested a few. Something like this:

$this->registerJs('
    jQuery( "#userprofile-use_gravatar" ).change(function() {
        $("#w0").yiiActiveForm("validate");
    });
    jQuery( "#userprofile-gravatar_email" ).keyup(function() {
        $("#w0").yiiActiveForm("validate");
    });
');

So this, you would think, would make Yii re-validate the form on every key press as the user types in the email box, or every time they click my checkbox… NOPE!

I about pulled my hair out. This is something that should be so simple…

Then after I went and watched fireworks with the family, got home, and started searching again, I stumbled across this issue (luckily).

First I reset the status as mentioned:

var $form = $("#w0"), 
data = $form.data("yiiActiveForm");
$.each(data.attributes, function() {
    this.status = 3;
});
$form.yiiActiveForm("validate");

Wow, it worked… This means that ‘validate’ is flat out broken

So I removed this little hack, and tried to validate just the attribute of the other input (my email). Here is the final code (note: I still needed the whenClient in my model, or it will break again lol)

$this->registerJs('
    jQuery( "#userprofile-gravatar_email" ).keyup(function() {
        $("#w0").yiiActiveForm("validateAttribute", "userprofile-gravatar_email");
    });
    jQuery( "#userprofile-use_gravatar" ).change(function() {
        $("#w0").yiiActiveForm("validateAttribute", "userprofile-gravatar_email");
    });
');

Finally, it works like a charm. I click the checkbox to use gravatar emails and boom, the email box pops up red. So I fill in an email, boom it goes green. I remove the email, it goes red. I uncheck the checkbox, email input goes green because it’s no longer required…


So we need to inspect the two functions, because validateAttribute is doing something that validate isn’t.

final note: You probably should use validateAttribute in most cases, unless you really need to re-validate the entire form. I suspect, if you have an AJAX call on a field that it will re-run it again too. This could cause unnecessary requests that could lead to performance issues in the future. However, validate is broken and needs fixed.

I had a problem where I have do an jquery.load to get an Yii active form.

Then in a wizard,module on the step validator, i wanted the step validator to trigger the yii form validation.

Then if i want to trigger only the form by javascript here is the way i found to work.

wizard.get(“#wizard_last_step”).setValidator(function() {

#---Begin this here is the part for trigger : activate yii client validation ---
jQuery('#my-form').yiiActiveForm('init');
jQuery('#my-form').yiiActiveForm('validate',true);
jQuery('#my-form').yiiActiveForm('validate');
#---End this here is the part for trigger---

if ($(‘#my-form’).find(“.has-error”).length > 0 ) { console.log($(‘#my-form’).find(“.has-error”).length ); return false; } else{

                    console.log($('#my-form').find(".has-error").length );
                        return true;
                  }

}