0

我正在尝试在更改密码功能上添加验证,但它不起作用。

我已经添加了

->add('repeat_password', [
                          'equalToPassword' => [
                                                'rule' => function ($value, $context) {
                                                    return $value === $context['data']['new_password'];
                                                },
                                                'message' => __("Your password confirm must match with your password.")
                                               ]
                         ]);

建模并在Users我的控制器中

$user = $this->Users->get($this->_User['user_id']);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $user = $this->Users->createEntity($user, ['password' => $this->request->data['repeat_password']]);
           // $verify = (new DefaultPasswordHasher)->check($this->request->data['old_password'], $user->password);
           // debug($verify);
           //if ($verify) {
            if ($this->Users->save($user)) {
                $this->Flash->success('The password has been changed');
                $this->redirect(['action' => 'index']);

            } else {
                $this->Flash->error('Password could not be issued');

            }
           }
        //   else {

           //    $this->Flash->error('Password Do not match');

        //   }
     //   }
    }

它无需验证即可保存数据。解决办法是什么 ?

4

2 回答 2

1

compareWith在没有彻底检查您的代码的情况下,我首先想到的是 CakePHP 3 已经为此目的提供了内置的验证器。

尝试如下设置验证规则:

$validator->add('repeat_password', [
    'compareWith' => [
        'rule' => ['compareWith', 'new_password'],
        'message' => __("Your password confirm must match with your password.")

    ]
]);

另外,检查数组中的new_passwordrepeat_password是否都设置为。true$_accessible

于 2015-12-13T10:33:27.093 回答
0
 public $validate = array(

    'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required'
        ),
        'min_length' => array(
            'rule' => array('minLength', '6'),  
            'message' => 'Password must have a mimimum of 6 characters'
        )
    ),

    'password_confirm' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'Please confirm your password'
        ),
         'equaltofield' => array(
            'rule' => array('equaltofield','password'),
            'message' => 'Both passwords must match.'
        )
    ),




)

请在您的模型中编写代码更多详细信息请查看以下链接 http://miftyisbored.com/a-complete-login-and-authentication-application-tutorial-for-cakephp-2-3/

于 2015-12-14T12:40:12.330 回答