I tried that, and unfortunately it is not providing the functionality I’m looking for.
For simplicity sake I created a very shot example:
From the Model:
class RegBillCompForm extends CFormModel
{
public $paydate;
public $billdate;
public function rules()
{
return array(
array('billdate, paydate', 'required'),
array('billdate', 'dateCompare'),
array('paydate', 'dateCompare'),
array('paydate, billdate', 'safe'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'paydate'=>'Pay Date:',
'billdate'=>'Bill Date:',
);
}
public function dateCompare()
{
if($this->paydate != null && $this->paydate != '' && $this->billdate != '' && $this->billdate != null)
{
if(strtotime($this->paydate) < strtotime($this->billdate))
{
$this->addError('paydate', 'Date cannot be prior to billdate.');
$this->addError('billdate', 'Date cannot be after to paydate.');
}
}
}
}
From the Controller:
public function actionIndex()
{
$f = new RegBillCompForm;;
if(isset($_POST['RegBillCompForm']))
{
$f->attributes = $_POST['RegBillCompForm'];
$this->handleAjaxValidation($f);//this just wraps ajax validation
}
$this->render('index', array('f'=>$f));
}
And finally the view
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'playground-form',
'enableAjaxValidation'=>true,
));
?>
<div class="row">
<?php echo $form->labelEx($f,'billdate'); ?>
<?php echo $form->textField($f,'billdate'); ?>
<?php echo $form->error($f,'billdate'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($f,'paydate'); ?>
<?php echo $form->textField($f,'paydate'); ?>
<?php echo $form->error($f,'paydate'); ?>
</div>
<?php $this->endWidget(); ?>
Whenever a one of the two fields is updated, then only THAT attribute is updated on the form. The desired effect I am looking for is to have both fields evaluated and updated.
A specific example that is not working is the following case:
User inputs a date of 5-1-11 for the bill date, then 4-1-11 for the paydate. Only the paydate field gives an error (and I would like both to show up as an error to draw attention to both fields). Upon seeing the error with the paydate field, they decide to change the bill date to 3-1-11, which satisfies the condition of having the paydate on or after the bill date. The ajax request is sent and no errors are found, but the paydate field remains red with an error as it was not being evaluated. I would like both fields to be updated and evaluated whenever either one changes a value, but the preceding code doesn’t not do this.
The actually case I am working on uses 2 models as opposed to this single model example I gave, which makes it even more complex. I realize that if COULD simply write a custom ajax script to compare the two values and have that mimic the ajax validation but adding / removing the correct classes which is what I’ll end up doing if I can find no other solution but I was hoping that this was supported already. These validation methods worked before the latest update with Yii, but no longer does with the way the form is not updated.