Here's how Yii implement AJAX validation in CActiveForm :
1)
Code for Form declaration generally found in _form.php
<? php $form = $this->beginWidget('CActiveForm', array(
'id'=>'sandy-form',
'enableAjaxValidation'=>true //switch on Ajax Validation on the client side
)); ?>
<? php echo $form->errorSummary($model); ?>
<div class="form-group">
<? php echo $form->labelEx($model,'title'); ?>
<? php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
<? php echo $form->error($model,'title'); ?>
</div>
<? php $this->endWidget(); ?>
2) put your validation code in model file found in models folder.
Model File
public function rules()
{
return array(
array('title', 'required'),
array('title', 'length', 'max'=>255),
);
}
3) Also put code corresponding controller file.
public function actionCreate()
{
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}
I hope you understand well.
Notes: Validation fires every time you tab out of a (changed) html form field.
From the javascript/jquery point of view it works with blur function.
1)
Code for Form declaration generally found in _form.php
<? php $form = $this->beginWidget('CActiveForm', array(
'id'=>'sandy-form',
'enableAjaxValidation'=>true //switch on Ajax Validation on the client side
)); ?>
<? php echo $form->errorSummary($model); ?>
<div class="form-group">
<? php echo $form->labelEx($model,'title'); ?>
<? php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
<? php echo $form->error($model,'title'); ?>
</div>
<? php $this->endWidget(); ?>
2) put your validation code in model file found in models folder.
Model File
public function rules()
{
return array(
array('title', 'required'),
array('title', 'length', 'max'=>255),
);
}
3) Also put code corresponding controller file.
public function actionCreate()
{
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(Yii::app()->getRequest()->getIsAjaxRequest()) {
echo CActiveForm::validate( array( $model));
Yii::app()->end();
}
}
I hope you understand well.
Notes: Validation fires every time you tab out of a (changed) html form field.
From the javascript/jquery point of view it works with blur function.