Attach ajax to a field

Hi,

I have a field in my register form that I want to check if a username exists or not, if it does exist its fine, it displays a message saying ‘this username has already been taken’ with the validation rules on Yii.

I want to attach another event to make it display a message if the username is available…

How can I grab the ajax response on validation from the request that yii sends and display my message rather than having to send two requests to the server (my custom one to check username and the default yii ajax validation)

Thanks

You can change the default code of the method ‘performAjaxValidation’ of the controller to something like below.

You can use the second parameter ‘attributes’ of CActiveForm::validate to check the attributes you want.

So check the attribute ‘username’ first.





         /**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

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

		{

                        

                        $validateAttributes = array('username');

                        //check only the username 

                        $userMessage = CActiveForm::validate($model,$validateAttributes);

                        

                        if (empty($userMessage)) //the username not exists

                             $userMessage = CHtml::tag('div',array(),'The username is available');  

                          

                        //get all other safe attributes for validation without 'username'

                        $validateAttributes = array_diff($model->getSafeAttributeNames(),$validateAttributes); 

                        echo $userMessage . CActiveForm::validate($model,$validateAttributes); 

  

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

		}

	}