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
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();
}
}