dougekui1518 2019-03-16 16:00
浏览 490

如何在laravel中的闭包函数中调用laravel验证规则?

I wish to skip the captcha validation if the account,which maybe a email or mobile, has been captcha validated.

so I write a colsure function. if the session('captcha_validated_reset_account') equals to the account which user input, it will skip the captcha validation.

if not, the program will call a laravel validation rule like "required|captcha"(captcha' comes from https://github.com/mewebstudio/captcha).

The problem is I don't know how to call validation rule in a closure,I tried return 'required|captcha' but it not work as intended.

'captcha' => function($attribute, $val, $fail)use($request) {
            if(!empty(session('captcha_validated_reset_account'))&&session('captcha_validated_reset_account')==$request->input('account')){
                return;
            }else{
                Rule::required... //and call a rule named 'captcha' to validate the captcha code from user input
                //return 'required|captcha';
            }
        }

Is any way I can call laravel validation rules in a closure function?

  • 写回答

1条回答 默认 最新

  • dpbtbcz6650 2019-03-16 16:08
    关注

    You should put the closure in an array:

    'captcha' => [ function($attribute, $val, $fail) use($request) {
                if(empty(session('captcha_validated_reset_account'))
                   && session('captcha_validated_reset_account') != $request->input('account')){
                    $fail($attribute . ' is required.');
                }
            }]
    

    Note that I've changed the condition.

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源