Data validation minLength does not work with Auth component for CakePHP

Say I have a user registration and I use the Auth component (/ user / register is allowed, of course).

The problem is that I need to set the minimum length check rule in the model, it does not work, because the Auth component hashes the password, so it is always larger than my minlength password, and it passes even if it is empty.

How to fix this problem? Thanks in advance!

+3
source share
2 answers

, (, "pw" ), Auth . , , password. beforeFilter() .

. , , , , CakePHP.

// this code would go after: if (!empty($this->data)  
//               and before: $this->User->save($this->data)

// validate the data
$this->User->set($this->data);
if ($this->User->validates()) {

    // hash the password
    $password_hash = $this->Auth->password($this->data['User']['pw'];
    $this->data['User']['password'] = $password_hash;
}
+6

hmm.. : . "pw2", . :

  • Auth hash pw2. ( , )
var $validate = array(
  'password' => array(
    'rule' => array('checkPwd')
  )
);
function checkPwd($check) {
  if(!isset($this->data[$this->alias]['password']) || 
     !isset($this->data[$this->alias]['pw2']))
     return 'Where are the passwords?';
  if($this->data[$this->alias]['password'] !== 
    Security::hash($this->data[$this->alias]['pw2'],null,true))
    return 'Passwords are not the same';
  if(strlen($this->data[$this->alias]['pw2']))<10)
    return 'Password not long enough';
  return true;
}

"value" = > '' .

0

Source: https://habr.com/ru/post/1743607/


All Articles