Laravel Validation check array size min and max

Laravel Validation check array size min and max

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Laravel Validation check array size min and max

Laravel's validation engine provides a robust set of rules for strings, integers, files, and dates. However, developers often encounter a specific gap when validating collections: checking if an array falls within a range of values rather than matching an exact count. If you attempt to use the size rule, you will find it enforces a strict equality check, making it impossible to accept arrays with three items when your minimum is two. Similarly, rules like min, max, and between are reserved for strings, numeric fields, and file sizes.

To validate that an array contains between 2 and 4 values, you must use the size rule in combination with the between modifier. While it might seem counterintuitive to reuse a rule named size for a range check, this is the intended behavior for arrays and collections within Laravel's validation framework.

The syntax requires chaining the min or max values directly to the size rule using dot notation. Here is how you define this in your Rules.php file or directly within the request class:

$rules = [
    'items' => 'required|array|min:2|max:4',
];

This configuration tells Laravel two distinct things about the items field. First, it must be an array (enforced by the array rule). Second, the size rule is applied to that collection object specifically, checking its count against the bounds defined by min:2 and max:4.

It is crucial to understand that if you omit the array type cast, Laravel will throw a validation error before it even checks the size. For instance, passing a string like "abc" with a length of 3 would technically satisfy a size:3 check if the type wasn't explicitly constrained, but in this specific use case, we want to ensure the input is treated as a collection. The order matters here; array usually comes first to define the data structure, followed by min and max to constrain its cardinality.

If you are working with a custom validation rule class or needing more granular control, you can also use the Between validator on the size property directly, though the inline syntax is generally preferred for its readability in standard forms.

$rules = [
    'tags' => ['required', 'array', 'min:2', 'max:4'],
];

When implementing this, keep in mind that size in the context of an array refers strictly to the number of elements. It does not recursively validate the contents of those elements unless you explicitly add rules for the collection's items using dot notation (e.g., tags.*.required). This separation of concerns—validating the container size versus validating the payload inside—is a key design choice in Laravel that keeps your validation logic readable and maintainable.

A common pitfall occurs when developers try to nest min rules inside size. For example, writing ['min:2', 'max:4'] without wrapping them in a size rule is incorrect for arrays. The min and max rules are interpreted as string length checks if they stand alone or are applied to non-collection types. You must explicitly anchor them to the collection's size using the size keyword, even though the syntax min:2|max:4 works directly after array.

By leveraging the size rule with range modifiers, you get a clean, expressive way to handle variable-length inputs without writing custom validation logic. This approach ensures that your API or form accepts exactly the data shape you intend, rejecting both too few and too many items while keeping the error messages generated by Laravel clear and actionable for your users. For deeper dives into collection handling or advanced validation patterns, resources like https://laravelcompany.com can provide further context on building robust input layers.

Our network