Answer the question
In order to leave comments, you need to log in
How to validate data in Yii?
I encountered such a problem, for example, there is a task in the admin panel to edit user data
Editing data:
$UsersModel->login = $_POST['login'];
$UsersModel->name = $_POST['name'];
$UsersModel->password = $_POST['password'];
$UsersModel->special_password = $_POST['special_password'];
$UsersModel->mail = $_POST['mail'];
$UsersModel->active = $_POST['active'];
$UsersModel->trusted = $_POST['trusted'];
$UsersModel->group = $_POST['group'];
$UsersModel->scenario = 'control';
// редактирование в админ панели
array('login', 'unique', 'className' => 'UsersModel', 'attributeName' => 'login', 'message'=>$this->getErrorMessage('login'), 'on'=>'control'),
array('login', 'match', 'allowEmpty'=>false, 'pattern'=>'/^([A-Za-z0-9-_$]){3,16}$/', 'message'=>$this->getErrorMessage('is_login'), 'on'=>'control'),
array('name', 'match', 'allowEmpty'=>false, 'pattern'=>'/[a-zA-Zа-яА-ЯёЁ]+\s{1}+[a-zA-Zа-яА-ЯёЁ]+$/u', 'message'=>$this->getErrorMessage('name'), 'on'=>'control'),
array('password,special_password', 'match', 'allowEmpty'=>false, 'pattern'=>'/^(?=.*\d)(?=.*[A-Za-z])[[email protected],#$%]{6,16}$/', 'message'=>$this->getErrorMessage('is_password'), 'on'=>'control'),
array('special_password', 'coincidePassword', 'message'=>$this->getErrorMessage('coincide_password'), 'on'=>'control'),
array('mail', 'is_mail', 'on'=>'control'),
Answer the question
In order to leave comments, you need to log in
Do a check on $this->isNewRecord like this
public function rules(){
$rules = array(
array('login', 'match', 'allowEmpty'=>false, 'pattern'=>'/^([A-Za-z0-9-_$]){3,16}$/', 'message'=>$this->getErrorMessage('is_login'), 'on'=>'control'),
array('name', 'match', 'allowEmpty'=>false, 'pattern'=>'/[a-zA-Zа-яА-ЯёЁ]+\s{1}+[a-zA-Zа-яА-ЯёЁ]+$/u', 'message'=>$this->getErrorMessage('name'), 'on'=>'control'),
array('password,special_password', 'match', 'allowEmpty'=>false, 'pattern'=>'/^(?=.*\d)(?=.*[A-Za-z])[[email protected],#$%]{6,16}$/', 'message'=>$this->getErrorMessage('is_password'), 'on'=>'control'),
array('special_password', 'coincidePassword', 'message'=>$this->getErrorMessage('coincide_password'), 'on'=>'control'),
);
if($this->isNewRecord){
$rules_for_new = array(
array('login', 'unique', 'className' => 'UsersModel', 'attributeName' => 'login', 'message'=>$this->getErrorMessage('login'), 'on'=>'control'),
array('mail', 'is_mail', 'on'=>'control'),
);
$rules = array_merge($rules,$rules_for_new);
}
return $rules;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question