TechKat's avatar

Multiple File Upload using Form Requests

So we all know that Form Requests helps cut down code by separating Validation from Controllers, and is called before the Controller is even ran.

I can make Form Requests work with single-file uploads, but a project I am working on will support multiple file uploads.

Is it possible to do this with the use of Form Requests? I want to be able to have each uploaded file go through the validation process.

0 likes
7 replies
jekinney's avatar

Yes, generally multiple files are in an array:

<input type="file" name="files[]">
<input type="file" name="files[]">
<input type="file" name="files[]">

then validate the array in your form request

return [
        'files.*' => 'mimes:jpeg,bmp,png', // or as required.
    ];

FYI, It calls the controller and as step one then calls the form request, if validation or access is true then continues down your code in the controller method otherwise returns the errors with redirect back().

TechKat's avatar

@jekinney There won't be a limit to how many files a person can upload, it'll just be the one file input with the multiple attribute.

1 like
jekinney's avatar

@TechKat That will be $49.95 via paypal for updating the code. ;)

Yes, generally multiple files are in an array:

<input type="file" name="files[]" multiple>

then validate the array in your form request

return [
        'files.*' => 'mimes:jpeg,bmp,png', // or as required.
    ];

FYI, It calls the controller and as step one then calls the form request, if validation or access is true then continues down your code in the controller method otherwise returns the errors with redirect back().

TechKat's avatar

@jekinney Okay, so the asterisk was the solution. However, the custom validation messages don't seem to be noticed. Upon dd'ing $request from the FormRequest class, it just outputs "validation.filesize", where filesize is a custom validator rule I made to check if the file is below the allowed limit.

TechKat's avatar

@jekinney Oh wait, never mind. The messages method also needed asterisks. It outputs the messages just how I wanted them. :) Thanks for the big help.

1 like
Ober4You's avatar

@TechKat can you please post the validation rule messages in custom Request..how did you do that?

Please or to participate in this conversation.