I'm using Yii's built in compare validator to compare two form attributes:
<div class="row ">
<?php echo $form->labelEx($model, 'newPassword', array('class'=>'control-label'));?>
<?php echo $form->passwordField($model, 'newPassword',array('class'=>'span5','maxlength'=>100)); ?>
<?php echo $form->error($model, 'newPassword'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'confirmPassword', array('class'=>'control-label'));?>
<?php echo $form->passwordField($model, 'confirmPassword',array('class'=>'span5','maxlength'=>100)); ?>
<?php echo $form->error($model, 'confirmPassword'); ?>
</div>
With the rules:
array('newPassword', 'length', 'max'=>100,'min'=>6),
array('newPassword', 'compare', 'compareAttribute'=>'username','operator'=>'!=', 'message'=>'Password must not be the same as username'),
array('newPassword', 'compare', 'compareAttribute'=>'confirmPassword','message'=>'Please enter the same password twice'),
array('confirmPassword', 'safe'),
When I first type the newPassword, the validation fires and the "enter the same" error message shows. If I then enter the confirmPassword correctly, it doesn't hide. If I then change the confirmPassword, and then change the newPassword to match., the error hides. It only seems to work when I change the newPassword field - so I have to fill in the form backwards for it to hide the error correctly.
Am I missing something?
[edit] I can force the validation to trigger on the field that has the compareAttribute rule, by using JavaScript to temporarily empty the field's value, triggering the validation, then reapplying the previous value and triggering validation again. Seems a little nuts though.