Multiple Attribute Error Handling

I have a model containing 3 related attributes" amount, elect2_amount and elect1_amount, and elect1_amount and elect2_amount should always sum to be equal to amount. I am trying to use model validation in order to ensure this condition is maintained and the goal is to have the ajax-validation always show an error on all 3 fields when this is not the case (execpt for when the user has not yet entered data into these fields).

Currently, my rule code looks like this:




            if($this->elect1_amount !== '' && $this->elect2_amount !== '' && $this->amount !== '')

            {

              if( ($this->elect1_amount + $this->elect2_amount) != $this->amount )

              {

                $this->addErrors(array(

                'amount'=>'The total amount for the first election and second election must total the amount of the contribution.',

                'elect1_amount'=>'The total amount for the first election and second election must total the amount of the contribution.',

                'elect2_amount'=>'The total amount for the first election and second election must total the amount of the contribution.'

                ));

              }

            }




This works find for validation on submission, but when using the ajax validation only the field that is being edited is validated. This means that on that field has an error placed on it and if another field is updated all other fields with an error are not updated. For example:

If a use enters the following values into their fields:

amount : 75

elect1_amount : 25

elect2_amount : 55

only the last field will receive an error. In addition, if another field is updated that now satisfies the

desired condition like so:

amount: 80

elect_1_amount : 25

elect_2_amount : 55

the error remain on the last field.

I believe this is due to how the active form sets up events. It would seem that when field leaves focus AND there is a change in the fields text that a ajax request is sent to validate only that field. I’m wondering if there is a way around this. If anyone has some thoughts I’d certainly be open to them.

I’ve also been running into this issue (that the AJAX validation only updates a single attribute and in cases where validation occurs across attributes, this means error messages stick around when they should not): http://www.yiiframework.com/forum/index.php?/topic/24292-update-multiple-attributes-on-client-validation/

Surely, there’s some way around this.