I have multiple file inputs
<input name="test[]" type="file">
<input name="test[]" type="file">
<input name="test[]" type="file">
I want to validate if the number of files uploaded doesn't exceed 2.
My validation rule is
'test.*' => 'bail|required|max_files_allowed:2|mimetypes:image/jpeg,image/gif,image/png,max:5000'
My validation rule is
Validator::extend('max_files_allowed', function ($attribute, $value, $parameters, $validator) {
//$attribute = "test.0"
$field_name = explode(".",$attribute)[0];
$files = $validator->getData()[$field_name];
return (count($files) <= $parameters[0]) ? true : false;
},"You reached the max number of files allowed.");
This is an example of \Log::info($validator->getData())
array (
'test' =>
array (
0 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '2018-05-23_1421.png',
'mimeType' => 'image/png',
'size' => 50101,
'error' => 0,
'hashName' => NULL,
)),
1 =>
Illuminate\Http\UploadedFile::__set_state(array(
'test' => false,
'originalName' => '2018-07-26_1752.png',
'mimeType' => 'image/png',
'size' => 105617,
'error' => 0,
'hashName' => NULL,
)),
),
)
When my validation rule fails it prints multiple error messages.
I'm assuming this is because it runs the validation rule against each element in the array.
What do I do so that the error message only displays once?
Or is my approach to counting the number of submitted files incorrect?