如何在蛋糕3中创建自定义验证规则

时间:2014-12-23 12:26:57

标签: cakephp-3.0

我正在开发Cake 3.我想创建一个自定义验证规则。 我想检查字段'password'是否等于'confirm_password'字段。

这是我的代码:

 public function validationDefault(Validator $validator) {
        $validator
                ->add('id', 'valid', ['rule' => 'numeric'])
                ->allowEmpty('id', 'create')
                ->add('email', 'valid', ['rule' => 'email'])
                ->requirePresence('email', 'create')
                ->notEmpty('email')
                ->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])
                ->requirePresence('password', 'create')
                ->notEmpty('password')
                ->notEmpty('confirm_password')
                ->add('confirm_password', 'custom', [
                    'rule' => function($value, $context) {
                        if ($value !== $context['data']['password']) {
                            return false;
                        }
                        return false;
                    },
                    'message' => 'The passwords are not equal',
        ]);

        return $validator;
    }

当我尝试'失败'表单提交时,代码保存,我没有错误。

我看了http://book.cakephp.org/3.0/en/core-libraries/validation.html#custom-validation-rules但没有帮助......任何人?

谢谢!

4 个答案:

答案 0 :(得分:18)

另一种将两个密码与CakePHP 3验证进行比较的内置方法可能是:

->add('confirm_password',
    'compareWith', [
        'rule' => ['compareWith', 'password'],
        'message' => 'Passwords not equal.'
    ]
)

您还可以将其添加到表定义中的validationDefault方法。

答案 1 :(得分:3)

好的,我已经找到了自己。对于那些坚持使用蛋糕模型的人:永远不要忘记将你的领域添加到$_accessible - 你的实体阵列中!

我的代码:

    /UsersTable.php;

    $validator->add('confirm_password', 'custom', [
        'rule' => function ($value, $context) {
            return false; // Its ment to go wrong ;)
        },
        'message' => 'Password not equal',
    ]);

    /User.php;

    protected $_accessible = [
        'email'            => true,
        'password'         => true,
        'bookmarks'        => true,
        'role_id'          => true,
        'confirm_password' => true,
    ];

答案 2 :(得分:2)

请尝试使用此代码,它肯定会有用

 $validator
     ->notEmpty('cpassword')
     ->add('cpassword', ['length' => ['rule' => ['minLength', 8],'message' => 'Password need to be at least 8 characters long',]])
     ->add('cpassword', 'passwordsEqual', [
                'rule' => function($value, $context) {
                    return isset($context['data']['cpassword']) &&
                     $context['data']['password'] === $value;      
                },
                'message' => 'The two password you typed do not match.',
        ]);

答案 3 :(得分:0)

public function validationResetpassword(Validator $validator){
    $validator
        ->requirePresence('password')
        ->notEmpty('password','Please enter Password')
        ->add('confirm_password', [
                    'compare' => [
                    'rule' => ['compareWith','password'],
                    'message' => 'Confirm Password does not match with Password.'
                    ]])
                    ->requirePresence('confirm_password')
                    ->notEmpty('confirm_password','Please enter Confirm Password')
                    ;

    return $validator;
}