MalcolmInTheCenter
MalcolmInTheCenter

Reputation: 1605

Laravel 5.4: How do I validate an array of file inputs with the same name?

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?

Upvotes: 0

Views: 331

Answers (3)

DevK
DevK

Reputation: 9962

Use max and array to enforce test being an array and so min will check for number of items in the array.

'test' => 'array|max:2',
'test.*' => 'bail|required|mimetypes:image/jpeg,image/gif,image/png,max:5000'

Upvotes: 1

Ahmed Nour Jamal El-Din
Ahmed Nour Jamal El-Din

Reputation: 1011

You may split your validation rule into two validation rules:

'test' => 'bail|required|max_files_allowed:2'
'test.*' => 'bail|required|mimetypes:image/jpeg,image/gif,image/png,max:5000'

and change your custom validation rule to:

Validator::extend('max_files_allowed', function ($attribute, $value, $parameters, $validator) {
    //$attribute is now "test" not "test.0" 
    $field_name = $attribute;
    $files = $validator->getData()[$field_name];


    return (count($files) <= $parameters[0]) ? true : false;
},"You reached the max number of files allowed.");

so with this modification, first you validate the array alone after that you validate each file using the second validation rule.

Note: not tested but it should work.

Upvotes: 0

Rehan
Rehan

Reputation: 4013

You can just return and display the first error message you get like this.

 if($validator->fails()){
        $error = $validator->errors()->first();

        $responseData = [
            'status' => 'failure',
            'msg' => $error
        ];

        return json_encode($responseData);
    }

Upvotes: 1

Related Questions