The issue with your validation rules is that the max:4 rule is being applied to the array itself, but it doesn't actually limit the number of files in the array. Instead, you should use the bail rule to stop on the first failure and then use the array rule with min and max constraints to ensure the number of files is within the desired range.
Here's how you can modify your validation rules to properly validate the number of uploaded files and show an error message:
public function rules(): array
{
return [
'file_inputs' => 'required|array|min:1|max:4',
'file_inputs.*' => 'required|file|mimes:pdf,jpg,png,gif|max:2048',
];
}
Explanation:
-
required|array|min:1|max:4: This ensures that file_inputs is a required field, it must be an array, and the array must contain between 1 and 4 items.
-
file_inputs.*: This applies the validation rules to each file in the file_inputs array, ensuring each file is required, is a valid file, has an allowed MIME type, and does not exceed the maximum file size of 2048 KB.
With these rules, if the number of files exceeds 4 or is less than 1, a validation error message will be shown. Additionally, each file will be individually validated for the specified constraints.