Laravel Validation check array size min and max

落花浮王杯 提交于 2021-01-17 06:42:14

问题


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.


回答1:


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




回答2:


You can use 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.

For more details, read validating array lengths in Laravel



来源:https://stackoverflow.com/questions/42342411/laravel-validation-check-array-size-min-and-max

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!