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.

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

报告相同问题?

悬赏问题

  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。