[['code','name'],'trim'],
['code','string','max'=>4],
[['code','name','status'], 'required'],
['email','email'],
[['tel','fax','address','contact','contact_tel','contact_mobile','status'],'safe']
['repassword', 'compare', 'compareAttribute' => 'password','message'=>'两次输入的密码不一致!'],
['age', 'compare', 'compareValue' => 30, 'operator' => '>='];
['age', 'default', 'value' => null];
['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024]
['level', 'in', 'range' => [1, 2, 3]];
['字段名', 'unique']
['username', 'string', 'length' => [4, 24]];
[
'primaryImage',
'image',
'extensions' => 'png, jpg',
'minWidth' => 100, 'maxWidth' => 1000, 'minHeight' => 100, 'maxHeight' => 1000
]
自定义rules:
['password', 'validatePassword'],
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, '账号或者密码错误!');
}
}
}