The order in compare validator

This is a form model:

class UserRegForm extends CFormModel


	public $email; // email is login account name, needs to be unique

	public $first;

	public $last;

	public $password;

	public $passwordRepeat; ... }

Here is the compare validator to make sure password and its repeat are the same.

array('passwordRepeat', 'compare', 'compareAttribute'=>'password')

Above code works


array('password', 'compare', 'compareAttribute'=>' passwordRepeat')

doesn’t work. every time I input the same password, e.g.: 123 and 123. It tells me the password is not same.

The only difference between the above code is the order of password and passwordRepeat.

Can you tell me what is the problem here? Does who compare with whom matter? Why?

Thank you!

I think you dont need

array('password', 'compare', 'compareAttribute'=>' passwordRepeat')


You don’t need both validations… why would you compare 2 times… if A=B and B=A ?

my logic would say to compare "password" to "passwordRepeat"

but in your code there is a space there that could make problems… - " passwordRepeat" - should be without the trailing space

I just use one comparison, not both way. Sorry didn’t make it clear.

It’s not the space. The following code doesn’t work. The same password 123 and 123 will result a not same alert.

			array('password', 'compare', 'compareAttribute'=>'passwordRepeat'),

Does the order matter?

It matters certainly if you use ajax validation… as it could happen that the validation is performed while there is nothing entered in the confirm field…

can you post your view file and the actionLogin() code

I know this is an old topic, but since there is no answer listed, here’s what I found to be the problem in the same situation.

The values in the form that was received in the POST would be mass assigned to the model. However, there was no validation rule for passwordRepeat, which meant that it would not be set during mass assignment. As such the comparison would fail. Adding this line in the rules should suffice:

array('passwordRepeat', 'safe'),