dpzr52746 2015-02-09 02:22
浏览 42
已采纳

CakePHP 3 - 比较密码

I have two field "password" (This field is in the database) and confirm_password (This field is not in the database)

Well, I need to compare if password == confirm_password.. but I'm not knowing create a custom validation to "confirm_password"... Would need to have this field in the database?

How do I do?

  • 写回答

2条回答 默认 最新

  • dongwu9063 2015-02-09 02:34
    关注

    Generally you can access all data in a custom validation rule via the $context argument, where it's stored in the data key, ie $context['data']['confirm_password'], which you could then compare to the current fields value.

    $validator->add('password', 'passwordsEqual', [
        'rule' => function ($value, $context) {
            return
                isset($context['data']['confirm_password']) &&
                $context['data']['confirm_password'] === $value;
        }
    ]);
    

    That being said, recently a compareWith validation rule was introduced which does exactly that.

    https://github.com/cakephp/cakephp/pull/5813

    $validator->add('password', [
        'compare' => [
            'rule' => ['compareWith', 'confirm_password']
        ]
    ]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?