I’m struggling with ajax validation. I have two models namely acl and user. Each have there own set of validation rules. I however use all input fields within one form.
My form looks as follows
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
)); ?>
<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,'region_id'); ?>
<?php echo $form->textField($model,'region_id'); ?>
<?php echo $form->error($model,'region_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($acl,'userName'); ?>
<?php echo $form->textField($acl,'userName',array('size'=>20,'maxlength'=>25)); ?>
<?php echo $form->error($acl,'userName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($acl,'passWord'); ?>
<?php echo $form->textField($acl,'passWord',array('size'=>20,'maxlength'=>25)); ?>
<?php echo $form->error($acl,'passWord'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($acl,'type'); ?>
<?php echo $form->textField($acl,'type',array('size'=>20,'maxlength'=>25)); ?>
<?php echo $form->error($acl,'type'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>20,'maxlength'=>25)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'surName'); ?>
<?php echo $form->textField($model,'surName',array('size'=>20,'maxlength'=>30)); ?>
<?php echo $form->error($model,'surName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>20,'maxlength'=>150)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email2'); ?>
<?php echo $form->textField($model,'email2',array('size'=>20,'maxlength'=>150)); ?>
<?php echo $form->error($model,'email2'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'cell'); ?>
<?php echo $form->textField($model,'cell'); ?>
<?php echo $form->error($model,'cell'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'tel'); ?>
<?php echo $form->textField($model,'tel'); ?>
<?php echo $form->error($model,'tel'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'fax'); ?>
<?php echo $form->textField($model,'fax'); ?>
<?php echo $form->error($model,'fax'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
In the UserController I have two ajax validation methods within the createAction method
This looks like this
public function actionCreate()
{
$model=new user;
$acl = new acl;
// Uncomment the following line if AJAX validation is needed
$this->performAjaxValidation1($acl);
$this->performAjaxValidation($model);
//$this->performAjaxValidation1($acl);
//$valid=$model->validate();
//$valid=$acl->validate() && $valid;
if(isset($_POST['user'], $_POST['acl']))
{
$model->attributes=$_POST['user'];
$acl->attributes=$_POST['acl'];
if($model->save() && $acl->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
'acl'=>$acl
));
}
Now the funny part is the ajax validation actually works, but only for one model at a time
If I call $this->performAjaxValidation1($acl); first this validation is done perfectly, but if I call $this->performAjaxValidation($model); first that is done successfully but the the second one is not performing as it should. If field is empty it turns green although it should turn red. Whichever ones of these I’m calling first that method will work 100%.
Validation methods look like this
protected function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation1($acl)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate($acl);
Yii::app()->end();
}
}
I’m desperate for a solution, please help