I want to validate my form with Ajax Validation method.So for I have made changes the code for view file like this.
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'customers-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstname,lastname,gender,email,contact_no,address,state,country'),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstname'); ?>
<?php echo $form->textField($model,'firstname',array('size'=>60,'maxlength'=>80)); ?>
<?php echo $form->error($model,'firstname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastname'); ?>
<?php echo $form->textField($model,'lastname',array('size'=>60,'maxlength'=>80)); ?>
<?php echo $form->error($model,'lastname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'gender'); ?>
<?php echo $form->radioButtonList($model,'gender',array('Male'=>'Male','Female'=>'Female'),array(
'labelOptions'=>array('style'=>'display:inline;padding-right:10px'),'separator'=>'',));?>
<?php echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'contact_no'); ?>
<?php echo $form->textField($model,'contact_no',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'contact_no'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'address'); ?>
<?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>120)); ?>
<?php echo $form->error($model,'address'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'state'); ?>
<?php echo $form->textField($model,'state',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'state'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'country'); ?>
<?php echo $form->textField($model,'country',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'country'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I have made the rules for this model is like this
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('firstname,lastname,gender,email,contact_no,address,state,country', 'required'),
array('firstname, lastname', 'length', 'max'=>80),
);
}
After all this code I came to see that all the fields are showing validation in a normal way but when going through Ajax Validation the only Firstname, Lastname, Email is showing ajax validation.Rest of the fields are not showing any ajax validation.Can someone tell me where I am doing wrong here?Any help and suggestion will be highly appreciable.
Edited
Here is the code for controller which is handling the Ajax validation
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='customers-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
Just use this in model’s actionCreate()
Rest of the files for view will be the same as above made in question.