unique and compare validator at one form

I am trying to put a UNIQUE and COMPARE validator in a same form.

As told, UNIQUE must be “CUniqueValidator can only be used for active record objects.”. That’s fine. so I used CDbActiveRecord.

Then I have another two textfields: password and confirmPassword. I set the rules like

array(‘password’,‘compare’,‘compareAttribute’=>‘confirmPassword’,‘message’=>‘The passwords you typed are different’),

And because in the database table there is no such field as “confirmPassword” but only “password”. So the code returns error like testForm.confirmPassword is not defined…

Seems in CDbActiveRecord, it’s not possible to use unexisted attributes? So how can I use this compare function in CDbActiveRecord instead of adding another field called confirmPassword in Database table ?(or  using CFormModel instead of CDbActiveRecord then Unique would be a problem)

Simply try to add a custom attribute to your AR class:

<?php


public $confirmPassword;

Btw. it’s CActiveRecord (without Db) ;).

Really helps!

Thanks very much, I will try…

It works, but you guys forgot to mention we need to override safeAttributes() to define the custom attribute with other default attributes to let the validation work.

E.g:



public function safeAttributes()


	{


		return 'confirmPassword, firstName, lastName, address, state, city, postcode,.......';


	}


You're right. Thanks for pointing out this.