0

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?

3 Answers 3

1

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);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

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'

1 Comment

Cleaner solution.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.