Laravel 最大验证器不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23938774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel max Validator not working
提问by randomor
I'm new to Laravel.
我是 Laravel 的新手。
Could someone explain why max
validator doesn't work as I expected in this case?
有人可以解释为什么max
在这种情况下验证器不能按我预期的那样工作吗?
$input = ["schoolSeatsTotal" => '2000'];
$rules = ['schoolSeatsTotal'=>'max:300'];
$validator = Validator::make($input, $rules);
$validator->fails(); //Expected: true, Actual: false.
回答by Laurence
You have schoolSeatsTotal
as a string. For string data, max value corresponds to the number of characters. You want to validate an integer instead.
你有schoolSeatsTotal
一个字符串。对于字符串数据,最大值对应于字符数。您想要验证一个整数。
So change
所以改变
$input = ["schoolSeatsTotal" => '2000'];
to
到
$input = ["schoolSeatsTotal" => 2000];
To make sure you are validating numbers - do this:
为确保您正在验证数字 - 执行以下操作:
$rules = ['schoolSeatsTotal'=>'numeric|max:300'];