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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:34:27  来源:igfitidea点击:

Laravel max Validator not working

phplaravellaravel-4

提问by randomor

I'm new to Laravel.

我是 Laravel 的新手。

Could someone explain why maxvalidator 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 schoolSeatsTotalas 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'];