【问题标题】:Validation doesn't work - CakePHP 3验证不起作用 - CakePHP 3
【发布时间】:2015-12-12 22:32:03
【问题描述】:

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

我已经添加了

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

        //   }
     //   }
    }

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

【问题讨论】:

  • 请贴出方法createEntity()的代码。
  • 你能打印出你的$user的值吗

标签: cakephp cakephp-3.0 cakephp-3.x


【解决方案1】:

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

尝试如下设置验证规则:

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

    ]
]);

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

【讨论】:

    【解决方案2】:
     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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-23
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多