dongyihao1099 2016-03-20 20:11
浏览 61
已采纳

将变量传递给验证器,Laravel

I have created a custom validator in AppSeriveProvider.php with code:-

Validator::extend('less_than', function($attribute, $value, $parameters, $validator) {
    $max_field = $parameters[0];
    $data = $validator->getData();
    $max_value = 100;
    return $value < $max_value;
});

Validator::replacer('less_than', function($message, $attribute, $rule, $parameters) {
    return str_replace(':field', $parameters[0], $message);
});

And my controller have this piece of code

$messages = [
    'bid.required' => 'Please enter the amount',
    'bid.less_than' => 'Insufficient balance',
];
$balance = 100;
$v = Validator::make($request->all(), [
         'bid' => 'required|less_than:$balance',
     ],$messages);

if ($v->fails()) {
    return redirect('newgame')
           ->withErrors($v)
           ->withInput();
    }else {
            echo "Success"
     }

I have to send balance variable to the validator and in the validator function I have to set $max_value (which currently have 100) to the value in $balance.

After searching in directories and looking to the code I cannot understand that what are the contents of $parameters variable because its 0 index is referred in max_field, how $validator->getData() works? and how $max_value is getting its value.

Please someone explain me all this or comment the link to respective problems.And help in solving this big problem.

  • 写回答

1条回答 默认 最新

  • dongou6632 2016-03-21 10:25
    关注

    To solve this problem I used the laravel's function dd() to see what are the contents of each variable. And then changed the custom validator in AppSeriveProvider.php to

    Validator::extend('less_than', function($attribute, $value, $parameters, $validator) {
                $balance = $parameters[0];          //$parameters array contain the $balance passed by validator::make()
                $data = $validator->getData();      //$data contain the $request->all()
                return $value < $balance;           //$value contain the bid set by user
    });
    
    Validator::replacer('less_than', function($message, $attribute, $rule, $parameters) {
                return str_replace(':field', $parameters[0], $message);
    });
    

    And the controller's code to

    $messages = [
                'bid.required' => 'Please enter the amount',
                'bid.less_than' => 'Insufficient balance',
    ];
    $balance = $user->balance;
    $v = Validator::make($request->all(), [
                'bid' => "required|less_than:$balance",  //this balance variable acts as the parameter array for extended validator class
         ],$messages);                    
    
    if ($v->fails()) {
                return redirect('newgame')
                            ->withErrors($v)
                            ->withInput();
    }else {
                echo "Success";
    }
    

    Explanation is provided in comments in code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装