Home > front end >  php - validate multiple file upload array in laravel
php - validate multiple file upload array in laravel

Time:04-12

I have a file upload input that allows multiple files to be uploaded. Right now, I am trying to find a way to validate those file uploads but not sure how to work it out.

Code Example

<input type="file"  name="file-1649657296668-0[]" access="false" multiple="true" id="file-1649657296668-0">

Let's say I uploaded multiple files on this input, how do I iterate and validate each of the files in this file input and ensure that it passes the validation check? The $request variable was unable to check on each array one by one on this file upload input.

CodePudding user response:

You can use wildcards * for array items.

<input type="file" name="files[]" multiple>
request()->validate([
    'files' => 'required',
    'files.*' => 'mimes:jpeg,jpg,png'
]);

CodePudding user response:

EDIT: Issue solved. Apparently, on the validator check, I missed the correct format of the input file name, since the ID is automatically generated from my formBuilder. Amended my code to follow the correct format.

Input file name example: file-1649657296668-0[]

E.g:

validated = $request->validate([
                       'file-1649657296668-0.*' => 'max:10000|mimes:pdf,xlsx,xls,docx,doc,png,jpg,jpeg',
                    ]);

Make sure to not miss the asterisk for input type files with multiple attributes, or else it won't validate the rest of the files.

  • Related