Ajax Validation Of Multiple $Model Within The Same Cactiveform

I’m struggling to validate multiple $models of the same class within the same _form. The Ajax is firing and applying the validation rule but the issue (from what I can understand) is that because all the HTML form elements all have the same id / name that the Ajax is taking the first textField it comes across and applying the rule.

The result is that I can empty one textField and receive a message the value is valid (rules states length must be greater than 4) because the Ajax is checking the other textField which is still populated.

I feel like I’m close but not quite understanding this final step. New to Yii and the forums :wink:

View/_form:




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

	'id'=>'handheld-form',

	'enableAjaxValidation'=>true,

)); ?>


<p class="note">Fields with <span class="required">*</span> are required.</p>

   

<?php echo $form->errorSummary(array($adminpass, $officephone)); ?>

<div class="row">

<?php 

     echo $form->labelEx($adminpass, 'value', array('label'=>$adminpass->name)); 

     echo $form->textField($adminpass, 'value', array('value'=>$adminpass->value, 'maxlength'=>64));

     echo $form->error($adminpass, 'value');

?>

</div>


<div class="row">

<?php 

     echo $form->labelEx($officephone, 'value', array('label'=>$officephone->name)); 

     echo $form->textField($officephone, 'value', array('value'=>$officephone->value, 'maxlength'=>64));

     echo $form->error($officephone, 'value');

?>

</div>



Controller:




public function actionAdmin()

{       

    $adminpass = new Configuration('hhAdmin');

    $officephone = new Configuration('hhAdmin');

          

    $this->performAjaxValidation($adminpass, $officephone);

....

}


protected function performAjaxValidation($adminpass, $officephone) 

{

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

{

    echo CActiveForm::validate(array($adminpass, $officephone));

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

}

}



Welcome to the community!

Your understanding must be exact, if not close.

Why do you need to have two instances of the same model ([font="Courier New"]Configuration[/font])? Maybe there are better solutions…

The model has a Configuration table, in which we insert Configuration variables based upon the client / app. So for example:

contact_phone_number, 123456789

contact_name, Mr Smith

etc,

Over time we expect to add to the table new variables and amend the existing values.

The idea of my view is to have a page where I can view these variables and amend the details accordingly. Some of the variables I have set rules on (min length etc.) and other not (using scenarios).

At the moment my Ajax is honouring the rules but because I have multiple models in the same view each HTML id is ‘Configuration[value]’ which my Ajax is picking the first instance it find (attached).

I think you are right - I have gone down a path which isn’t best practice and I need to rethink my approach.

CFormModel maybe a better way to go and fetch the variables that way.

When you need to have inputs for several instances of the same model, you may want to go with tabular input

Many thanks that looks promising.