public function beforeValidate()
{
foreach ($this->attributes as $name => $val)
$this->$name = trim($val);
return true; // Required, else validation will not be performed
}
Check the LoginForm model in the blog tutorial for example(authenticate rule):
class LoginForm extends CFormModel
{
...
public function rules()
{
return array(
...
array('password', 'authenticate'),
);
}
public function authenticate($attribute,$params)
{
if(!$this->hasErrors()) // we only want to authenticate when no input errors
{
$identity=new UserIdentity($this->username,$this->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($identity,$duration);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('username','Username is incorrect.');
break;
default:
$this->addError('password','Password is incorrect.');
break;
}
}
}
...
}