Can Not Enable Ajax Validation.....any Ideas...?

Here is my form:

is not in the main layout. but definitely included jquery…


	public function actionIndex()

	{


         //create new user


         $model = new Users; 

         $profile = new Profile;


       if(isset($_POST['ajax']))

	{

        echo CActiveForm::validate($model);

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

	}

.....

And the form:


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

	'id'=>'first-form',

       'method'=>'post',  

       'enableAjaxValidation'=>true,

)); ?>

Right now it’s not doing ajax validation at all…And i can’t see any AJAX request via firebug either…

Hi Jiaming.

It is confused the ‘enableAjaxValidation’=>true does not trigger the ajax on submit button.

You can see ajax activities in firebug when tab out of your form fields!

I have the same confuse with you before one hour! :)

Where are the model attributes that you want to validate? You just instanciate a new model.

You should assign the attributes first from the $_POST array if they are set.

Use it like below:


	public function actionIndex()

	{


         //create new user


         $user = new Users; 

         $profile = new Profile;


         $this->performAjaxValidation(array($user, $profile));


         if (isset($_POST['User'])) {

             $user->attributes = $_POST['User'];

             ........

         }

.....

Also in this controller you will need (may already be there):


/**

     * Performs the AJAX validation.

     * @param CModel the model to be validated

     */

    protected function performAjaxValidation($validate) {

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

            echo CActiveForm::validate($validate);

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

        }

    }

And the form:


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

	'id'=>'first-form',

       'method'=>'post',  

       'enableAjaxValidation'=>true,

       'clientOptions' => array(

            'validateOnSubmit'=>true,

            'validateOnChange'=>true,

            'validateOnType'=>false,

        ),

)); ?>