I have a working Active Form, which can be submitted, and validated via Yii PHP. However, I would like to determine if the form is valid, when clicking a Next button.
I can pass error messages to the user via this:
$("#form").yiiActiveForm("validate", true);
But this function doesn't return anything; I don't know if there are indeed any errors or not. I tried this:
$error_count = document.getElementsByClassName("help-block").length
but this does not work; the errors are counted before the UI has updated. If I press the button again, a second time, then error_count is what I'd expect.
This doesn't seem to do anything:
$("#form").yiiActiveForm("validate");
I also tried this:
$('#form').on('afterValidate', function (event, messages, errorAttributes) {}
But this is only triggered after the fact so I'm not sure what to do.
Any advice would be appreciated.
afterValidateorbeforeSubmitevent that Yii provides instead of defaultsubmitevent. ThebeforeSubmitevent is triggered only if form passes all validations.trueasforceValidateparam. It setssubmittingflag to true and because of that thevalidatemethod usessetTimeoutto delay the UI update. UsingafterValidateevent to count errors should help. The array of attributes with validation error is passed as third parameter to callback for that event so you don't need to count.help-block.truebut nothing visibly seemed to happen. Could you share how to useafterValidate?