I have such code, but don’t know how to do them in a rules array, maybe some one can help…
/**
* Add errors after validation, email is used for registration and account already activated.
*
* @return bool
*/
public function afterValidate()
{
if ($this->getScenario() == 'form') {
$oUser = User::model()->findByAttributes(array('email' => $this->getEmail()));
if ($oUser) {
if ($oUser->getConfirmStatus()) {
$this->addError('email', 'Аккаунт с данным email уже активирован.');
}
$this->attributes = $oUser->attributes;
} else {
$this->addError('email', 'Данный email не использовался для регистрации.');
}
}
return parent::beforeValidate();
}
Check email we can as unique rule, but how can we check $oUser->getConfirmStatus() in rules?
You want to validate the email by validating that it is already confirmed.
Suppose that you call your validation function ‘validateEmailConfirmed’. So you need a rule like:
array('email','validateEmailConfirmed','on'=>'form')/code]
Then define the method:
[code]function validateEmailConfirmed($object,$attribute) {
$valid=true;
$oUser = User::model()->findByAttributes(array('email' => $object->$attribute));
if ($oUser) {
if ($oUser->getConfirmStatus()) {
$object->addError($attribute, 'Аккаунт с данным email уже активирован.');
$valid=false;
}
$object->attributes = $oUser->attributes;
} else {
$object->addError('$object, 'Данный email не использовался для регистрации.');
$valid=false;
}
}
return $valid;
}
My code is working well for me. I want simply create it in ‘rules’ method, and don’t want create specific ‘before\after validate’. So main question is how create x2 rules from afterValidate in rules() method (of course use method is not our solution).