I want to add additional attribute to ActiveRecord which is not present in database, but I want it only for validation.
Examples of use:
- We have a form for adding user and there should be password2 which should be validated by rule: [tt]array('password2', 'compare', 'compareAttribute'=>'password')[/tt].
- We want to add captcha to form which inserts a record to db.
-
How should I modify the model?
-
Should I use activeLabel and activePasswordField for "validating only" field like 'password2' ?
-
When should perform e.g. md5 hashing? maybe onBeforeSave?
I found the way to do this but it doesn't seem to be very elegant…
class User extends CActiveRecord { protected function afterConstruct() { $this->setAttribute('password2', ''); } public function hasAttribute($name) { if($name == 'password2') return true; return parent::hasAttribute($name); }
and in Controler:
$user=new User; if(isset($_POST['User'])) { $user->attributes=$_POST['User']; $user->setAttribute('password2', $_POST['User']['password2']); if($user->save()) $this->redirect(array('list','id'=>$user->id)); }
Maybe there is simpler way to do this?