Ajax Validation Is Not Performed For Second Model

I have created a form using two models name customer and shippingaddress, now the ajax validation is working for the first model elements, and for the second model.

This is the form code


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

	'id'=>'customer-form',

	'enableAjaxValidation'=>true,

)); ?>    

    

    <div class="requid">Required Field <span style="color:#d32c3b;">*</span></div>


	<?php echo $form->errorSummary(array($model,$model2));?>

<div class="noncolor bor" style="margin-top:2%">         

    

    <?php if(Yii::app()->user->hasFlash('error4')): ?>

 

		<div class="flash-success">

			<?php echo Yii::app()->user->getFlash('error4'); ?>

		</div>

		<?php endif; ?>

    <table class="tbl_fieldset" >

	<tbody>

	<tr><td></td></tr>

		<tr style="display:none" >

			<th class="name">Date Added</th>

			<td >

		<?php  if($model['date_added']==''){echo $form->textField($model,'date_added',array('size'=>30,'value'=>$date));}

		else{echo $form->textField($model,'date_added');}

		?>

		<?php echo $form->error($model,'date_added'); ?></td>

			<td>&nbsp;</td>

		</tr>

		<tr >

			<th class="name" >First Name <span style="color:#d32c3b;">*</span></th>

			<td >

			<?php echo $form->textField($model,'firstname',array('size'=>35,'maxlength'=>50)); ?>           

            <?php echo $form->error($model,'firstname'); ?></td>

			<td>&nbsp;</td>

		</tr>	

        <tr >

			<th class="name" >Shipping Name<span style="color:#d32c3b;">*</span></th>

			<td >

			<?php echo $form->textField($model2,'name',array('size'=>35,'maxlength'=>50)); ?>           

            <?php echo $form->error($model2,'name'); ?></td>

			<td>&nbsp;</td>

		</tr>

        

        <tr >

			<th class="name" >Shipping Country <span style="color:#d32c3b;">*</span></th>

			<td >		

			<?php   					

            echo $form->dropDownList($model2,'country_id',CHtml::listData(Country::model()->findAll(), 'country_id', 'country_name','');

                         ?>        

		<?php echo $form->error($model2,'country_id'); ?></td>

			<td>&nbsp;</td>

		</tr>

		<tr> 	

        <td>&nbsp;</td>	

		<td ><?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class' => 'sub', 'onclick'=> 'assign_state_city()',)); ?>  <td></tr>

	</tbody>

</table>      

    

</div>

    


<?php $this->endWidget(); ?>






The validation rules are defined for two models in their respective models.

The controller code:

for ajaxvalidation


			

	$this->performAjaxValidation($model,$model2);

        

	protected function performAjaxValidation($model,$model2)

	{

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

		{

			echo CActiveForm::validate($model,$model2);

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

		}

	}



The ajax validation is only working for the first model only…

Need some help…

Hi,

the best way is you can create the custom model and it’s extends the


 CForm model

if you want to validate all field you want to add the public variable and defind them…


class ModleName extends CFormModel

{

	public $date_added;

	


	/**

	 * Declares the validation rules.

	 * The rules state that email and password are required,

	 * and password needs to be authenticated.

	 */

	public function rules()

	{

		return array(

			

			array('date_added,', 'required'),


			

		);

	}

}



and use this model create the action and render the page view file




public function actionCreate(){

$model = new Modlename()

$this->render('your view file',array('model'=>$model))

}



and your_form.php




<div class="form">


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

    'id'=>'banner-management-_form-form',

    'enableAjaxValidation'=>false,

)); ?>


    <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,'date_added'); ?>

        <?php echo $form->textField($model,'date_added'); ?>

        <?php echo $form->error($model,'date_added'); ?>

    </div>


   


    <div class="row buttons">

        <?php echo CHtml::submitButton('Submit'); ?>

    </div>


<?php $this->endWidget(); ?>


</div><!-- form -->

hope you can got the my point…

it’s very simple…


echo CActiveForm::validate($model,$model2);

Must be


echo CActiveForm::validate(array($model,$model2));

As API say $models - a single model instance or an array of models.

Or if you dont trust CActiveform, you can reform CModel::validate() response:




if(Yii::app()->request->isAjaxRequest)

        {

            if(!$model->validate()){

                foreach($model->getErrors() as $attribute=>$errors)

                    $error[CHtml::activeId($model,$attribute)]=$errors;

                echo CJSON::encode($error);

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

              }

        }