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条)

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目