Validating Array Lengths in Laravel
Arie Visser • December 15, 2019
laravel php validationOne of the cool things of Laravel's validation rules, is that you can apply certain rules to different data types.
When checking the minimum and maximum size of an array, you can use the min
, max
, between
, and size
rules, which you can also apply to strings and numerics.
When you have to validate that an array contains at least five users, you can apply the min
rule:
'users' => 'array|min:5'
In the same way, you could validate that the array contains not more than ten users:
'users' => 'array|max:10'
In case you want to validate that an array contains at least five, but not more than ten users, you can apply the between
rule:
'users' => 'array|between:5,10'
This are all the rules you need to validate array lengths.