Cform and enableAjaxvalidation

I have a form that is generated using the form builder - cform class. I want to enable ajax validations on this form so that I can validate input using the model rules specified in the rules function of the model. Kindly help.

I got it working.

Here is what I did:

On the form specification array, .i.e. where you specify your elements, buttons e.t.c. add the following array as part of the items of the main array:




'activeForm' => array(

                      'class' => 'CActiveForm',

                      'enableAjaxValidation' => true,

                      'enableClientValidation' => false,

                      'id' => form-id,

                     ),



Note that either enableAjaxValidation or enableClientValidation should be set to true but not both.

Ensure that jquery get included by checking with your client side source using firebug or any client source code viewer.

If it doesn’t get loaded use:




Yii::app()->clientscript->scriptMap['jquery.js'] = true;  



Or try this:




       $cs=Yii::app()->clientScript;

        $cs->registerCoreScript('jquery');



This worked for me.

If you are using CJUIDIALOG dialog widget or rather posting your values via ajax, sometimes jquery gets included multiple times and so you need to set this:




if (Yii::app()->request->isAjaxRequest) 

      Yii::app()->clientscript->scriptMap['jquery.js'] = false; 



This will make jquery continue to function despite the many trips to the server via ajax.

And a very important thing is Dont forget to uncomment these lines in your controller action. ;)




// uncomment the following code to enable ajax-based validation


		if(isset($_POST['ajax']) && $_POST['ajax']==='form-id')


		{


			echo CActiveForm::validate($model);


			Yii::app()->end();


		}



But the id’s form must be between single quotes isn’t in? like the rest of the parameters.

This isn’t work for me!!! i have not idea what’s happend!!

any idea???