本文介绍了CakePHP 3 - 比较密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有两个字段password(此字段在数据库中)和confirm_password(此字段不在数据库中)

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

如果password == confirm_password ..但我不知道创建一个自定义验证confirm_password...需要在数据库中有这个字段吗?

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?

如何

推荐答案

通常,您可以访问。 $ context 参数,其中它存储在 data 键,即 $ context ['data'] ['confirm_password'] ,然后可以与当前字段值进行比较。

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;
    }
]);

话虽如此,最近还有一个 compareWith 验证规则。

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

这篇关于CakePHP 3 - 比较密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:04