Related Field

I am using the CActiveForm and using ajax validation. I have a scenario where 2 or more fields are related and their valid inputs are dependant on each other. One example of this is that I have a model with 2 different date values. Whenever I edit one value, the ajax validation for that value is preformed, but only on that value. This works fine, except in situations where changing another field makes the current invalid value valid.

For example, say you have two fields, ‘startDate’ and ‘endDate’. End date cannot be prior to start date. You enter a value for start date of 5/1/11 and a value for end date of 4/1/11. endDate gives an error (as it should). At this point, changing endDate to a later value such as 6/1/11 fixes the issue and a new ajax validation check turns the field green and removes the error. The problem I am having, is I would like to have the field re-validated if startDate changes as well. So, if instead of altering the endDate field, you decided to change startDate to 3/1/11 endDate would be re-validated and found to be okay.

Does anyone have any suggestions on how to go about this or even if you could point me to the right direction that would be great. This use to work before the last Yii release, but with the changes to how the validation works I’m finding it does not.

One solution would be to create a custom validator… and set both date to the same custom validator… where you would compare the dates and set errors accordingly…

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.