我有两个字段“密码”(这个字段在数据库中)和confirm_password (这个字段不在数据库中)
我需要比较一下密码== confirm_password..。但我不知道为“confirm_password”创建一个自定义验证..。需要在数据库中包含这个字段吗?
我该怎么做?
发布于 2015-02-09 02:34:39
通常,您可以通过自定义验证规则参数访问$context中的所有数据,该参数存储在data键(即$context['data']['confirm_password'] )中,然后您可以将其与当前字段值进行比较。
$validator->add('password', 'passwordsEqual', [
'rule' => function ($value, $context) {
return
isset($context['data']['confirm_password']) &&
$context['data']['confirm_password'] === $value;
}
]);尽管如此,最近引入了一个compareWith验证规则,它正是这样做的。
https://github.com/cakephp/cakephp/pull/5813
$validator->add('password', [
'compare' => [
'rule' => ['compareWith', 'confirm_password']
]
]);发布于 2017-03-30 06:39:07
现在,在验证器类中有一个方法调用sameAs,用于版本3.2或grater。
$validator -> sameAs('password_match','password','Passwords not equal.');请参阅API接口
发布于 2021-08-23 10:39:57
我知道这是迟了的回答,但会对其他人有所帮助。
// Your password hash value (get from database )
$hash = '$2y$10$MC84b2abTpj3TgHbpcTh2OYW5sb2j7YHg.Rj/DWiUBKYRJ5./NaRi';
$plain_text = '123456'; // get from form and do not make hash. just use what user entred.
if (password_verify($plain_text, $hash)) {
echo 'Password is valid!';
} else {
echo 'Invalid password.';
}或
$hasher = new DefaultPasswordHasher();
$check = $hasher->check($plain_text,$hash); // it will return true/falsehttps://stackoverflow.com/questions/28401893
复制相似问题