N
N
nepster-web2013-12-25 22:08:10
Yii
nepster-web, 2013-12-25 22:08:10

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';

I come across the following:
Data comes in and goes to validation, validation honestly works out and I get a problem:
- I already have such a login - I already have
such mail
- I store passwords in cryp () and cryp is displayed in the form, but the password can be reset by entering for example 123456 and the password will be like this. However, if the administrator does not want to reset the password, then the crypt arrives and the validation does not pass, at least in terms of the length of the characters.
Please tell me how to solve the problem with the validation of user data. More precisely, how to edit the data in such a way that if the login or mail does not change, then the rule does not work such a login / email already exists.
Validation rules:
// редактирование в админ панели 
            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

[[+comments_count]] answer(s)
T
TekVanDo, 2013-12-25
@nepster-web

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 question

Ask a Question

731 491 924 answers to any question