Client Validation and Unique Validator

Hello, I’m new to Yii and trying to make registration form. It seems that client validation can’t check username field for uniqueness. Is there any way to do this not by pressing submit button, but simply changing focus from one field to another?

  1. In your form(_form.php) enable ajax validation,

'enableAjaxValidation'=>true,

  1. In your controller, in actionCreate() and actionUpdate() method uncomment below line

$this->performAjaxValidation($model); 

  1. In Model rules(), add unique validation

array('username', 'unique'),

Thank you for reply. But my question is a bit different: how to trigger AJAX validation after changing fields of input? Because it seems, that validateOnChange applies to client validation, which ignores unique validator, and AJAX by now triggers only by pressing submit button

Follow my above given step it will work.

Also check your form(views/user/_form.php) id ‘user-form’ must be match in your controller function performAjaxValidation($model)


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

	'id'                        => 'user-form',

	'enableAjaxValidation'      => true,

        'enableClientValidation'    => true,

        'clientOptions'=>array(

            'validateOnSubmit'=>true

         )

)); ?>

In your controller you will find below method whic has condition $_POST[‘ajax’]===‘user-form’.


       /**

	 * Performs the AJAX validation.

	 * @param $model the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

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

		{

			echo CActiveForm::validate($model);

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

		}

	}