Laravel 验证检查数组大小的最小值和最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42342411/
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 Validation check array size min and max
提问by Happy Coder
I just need to check in Laravel validation that an array contains minimum 2 values and maximum 4 values. Is there any default validation rule can be used for this ? The size
element checks for exact size match and the other rules likes min
and max
are for strings, digits and file size.
我只需要在 Laravel 验证中检查一个数组包含最少 2 个值和最多 4 个值。是否有任何默认验证规则可用于此?该size
元素检查精确的大小匹配,其他规则喜欢min
和max
用于字符串、数字和文件大小。
回答by piscator
You can use between
.
.
https://laravel.com/docs/5.2/validation#rule-between
您可以使用between
. .
https://laravel.com/docs/5.2/validation#rule-between
For example:
例如:
'users' => 'required|array|between:2,4'
It is not explicitly stated in the docs, but it works with arrays, just like size
.
它在文档中没有明确说明,但它适用于数组,就像size
.
For more details, read validating array lengths in Laravel
有关更多详细信息,请阅读在 Laravel 中验证数组长度
回答by Isaac Limón
A little bit late:
有点晚了:
return [
"ticket_photo" => ["required","array","min:2","max:4"], // validate an array contains minimum 2 elements and maximum 4
"ticket_photo.*" => ["required","mimes:jpeg,jpg,png,gif"], // and each element must be a jpeg or jpg or png or gif file
];
laravel 5.6
拉拉维尔 5.6