Search code examples
phplaravellaravel-validation

How to Validate an array in Laravel


I am trying to apply the suggestions provided in this question How to validate array in Laravel?

So my validation is

'topics' => 'required|array'

Topics are required

This works well especially if topics is an array greater than 1

unfortunately is I pass [] an empty array the validation fails

How can I validate that the input is an array, Its not null and empty array is allowed?

Below attempt fails

 'topics' => 'required|array|min:0',

Topics are required

Below works, the problem is that even null values are permitted

 'topics' => 'array',

Solution

  • you can use present validation

    The field under validation must be present in the input data but can be empty.

     'topics' => 'present|array'