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.
-
I do not believe there currently is a validation rule for this. You can however create a custom validation rule.Jerodev– Jerodev2017-02-20 10:51:24 +00:00Commented Feb 20, 2017 at 10:51
-
which laravel version do you use?darthsoup– darthsoup2017-02-20 10:52:49 +00:00Commented Feb 20, 2017 at 10:52
-
I am using Laravel 5.2Happy Coder– Happy Coder2017-02-20 10:54:27 +00:00Commented Feb 20, 2017 at 10:54
-
Ah Ok. You need to make your own validator check. The default size validator only checks the value and not min/max.darthsoup– darthsoup2017-02-20 11:00:06 +00:00Commented Feb 20, 2017 at 11:00
Add a comment
|
2 Answers
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
1 Comment
N Kumar
Now It is in Documentation too 5.6