Ajax Validation of CJuiAutoComplete

I can’t work out how to do this.

So far I have:




		<div class="row">

			<?php echo $form->labelEx($model->cat,'name'); ?>

			<?php

				$this->widget('zii.widgets.jui.CJuiAutoComplete', array(

					'name'=>'Cat_name',

					'value'=>$model->cat->name,

					'source'=>'/site/cats',

					'options'=>array(

						'showAnim'=>'fold',

					),

					'htmlOptions'=>array(

						'style'=>'height:20px;'

					),

				));

			?>

			<?php echo $form->error($model->cat,'name', array("validateOnChange"=>true)); ?>

		</div>




but it does not post back for ajax validation.

As you can see I’ve tried forcing the validation, but no joy.

Validation works fine on submit.

Ajax validation is working for other elements on the page.

Fixed.

The problem wasn’t CJuiAutoComplete but in the ajax validation code.

You have to validate like this if there is a related model attached.




$errors =  CActiveForm::validate(array($model, $model->related_model));



I’m having a similar problem. I try to validate a CJuiAutoComplete:


<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(

'model'=>$model,

'attribute'=>'country',

'name'=>'country',

'options'=>array(

'minLength'=>1,

),

'source'=>Yii::app()->asdp->countriesAutocomplete,

)); ?>

Rule is as follows:




array('country', 'CountryValidator', 'message'=>'Bitte ein Land aus der Liste auswählen'),



where Validator looks like this




<?php

class CountryValidator extends CValidator

{

protected function validateAttribute($model,$attribute)

{

$value=$model->$attribute;

if(Country::model()->findByAttributes(array('country'=>$model->country))==null)

$model->addError($attribute, $this->message);

}

}

?>



The validator works fine on submission of the form. But ajax validation is not working on this field at all.

Any ideas? Thanks a lot.

Solved it: if I donot set the name property


'name'=>'country',

it works. Why is that?

Yii generate field names like somemodel[someattribute] which makes it possible to access all fields of the posted form from an array.

/Tommy