Check if form is valid or not?

Hi,

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




$("#form").on("submit",function(e){

e.preventDefault();


// How to check if form is valid?<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' 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(

	'id'=>'form', 

    'htmlOptions'=>array('class'=>'dialog-form'),      

	'action'=>$this->createUrl("category/save"),

	'enableClientValidation'=>true,

	'errorMessageCssClass'=>"error",		

	'clientOptions' => array(

			'validateOnSubmit'=>true,

			'validateOnChange'=>false,

			'afterValidate'=>'js:submitForm'	

	)

)); ?>

 

Try this.

http://www.yiiframework.com/wiki/750/how-to-implement-form-events/

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) {

           

            $("#form").ajaxSubmit({

                success: function(response) {

                    if (ajaxValidate(response)) {

                        $("#form").dialog("close");

                        loadList(page);

                    }

                }

            });

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

            e.preventDefault();

        });