Check if form is valid or not?


I want to check if form is valid or not on client side. How can I do it?

In yii1.x we had option “afterValidate”. But on yii2 I can’t find any option like that. So I am using



// How to check if form is valid?<img src='' class='bbc_emoticon' alt='???' />


Please help.

There still is afterValidate in Yii 2.

Also, Yii 2 has client validation on by default, I believe it creates Javascript rules on the fly based on your validation methods; however if you have custom methods then it probably wouldn’t include those.

Obviously make sure you’re still validating it on the server side as well.

On top of that there is also AJAX Validation for validation that has to be done server side, but you want to make it act like client side validation.

No! The link you have provided is about afterValidate event on Server side.

I am looking for Yii2.x code for following Yii1.x code:

<?php $form=$this->beginWidget('CActiveForm', array(






	'clientOptions' => array(





)); ?>


Try this.

A little extra code. You were halfway there…

var form = $(this);

                if(form.find('.has-error').length) {

                        return false;


Before submit event has solved my problem.

$("#form").on("beforeSubmit", function(e, m, d) {



                success: function(response) {

                    if (ajaxValidate(response)) {






        }).on("submit", function(e) {