CakePHP 验证规则匹配 field1 和 field2

发布于 2024-11-26 09:31:51 字数 112 浏览 1 评论 0 原文

我正在制作一个密码重置表单,其中包含两个字段:password1 和password2。用户输入新密码,然后再次重新输入新密码。

我不确定如何制定验证规则来比较字段中的两个值并查看它们是否相同。

I am making a password reset form, which contains two fields: password1 and password2. The user enters their new password and then re-types their new password again.

I'm not sure how to make a validation rule that will compare the two values from the fields and see if they're the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2024-12-03 09:31:51

恕我直言,在这种情况下创建单独的规则更麻烦而不值得。如果您想编写“纯”CakePHP 代码,您可以这样做,但比较控制器中的字段并在它们不匹配时手动使其中之一无效更容易:

if( $this->data[ 'User' ][ 'password1' ] != $this->data[ 'User' ][ 'password2' ] ) {
    $this->User->invalidate( 'password2', "The passwords don't match." );
}

IMHO it's more trouble than worth to create a separate rule in this case. You could, if you want to code "pure" CakePHP, but it's easier to just compare the fields in the controller and invalidate one of them manually if they don't match:

if( $this->data[ 'User' ][ 'password1' ] != $this->data[ 'User' ][ 'password2' ] ) {
    $this->User->invalidate( 'password2', "The passwords don't match." );
}
无敌元气妹 2024-12-03 09:31:51

如果您使用 Auth 组件,那么您需要在控制器中对第二个密码进行哈希处理,因为密码将自动进行哈希处理。

要比较 2 个字段,您需要编写自定义验证规则: http://bakery.cakephp.org/articles/aranworld/2008/01/14/using-equalto-validation-to-compare-two-form-fields(另请阅读评论,因为教程本身有点旧)

if you are using Auth component then you need to hash the second password in the controller, because the password will be automatically hashed.

To compare 2 fields, you need to write a custom validation rule: http://bakery.cakephp.org/articles/aranworld/2008/01/14/using-equalto-validation-to-compare-two-form-fields (read the comments also, because the tutorial itself is kind of old)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文