J
J
jallvar2020-11-25 13:39:47
Validation
jallvar, 2020-11-25 13:39:47

How to validate an array of files in laravel?

Hello.
I have a site component - messages. Where can I attach images and office files.

in the view: In the controller:
<input type="file" name="attachments[]" multiple>

$request->validate([
            "item_id" => "required|exists:items,id",
            "message" => "required|max:2000",
            "attachments.*" => "nullable|mimes:jpeg,png,gif,docx,xlsx,ppsx",
        ]);


I get an error
The attachments.0 must be a file of type: jpeg, png, gif, docx, xlsx, ppsx.

I upload files strictly according to the memo type. I checked the online memo file type - everything converges.
But the file is not loading. Swears at everything

What could be the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jallvar, 2020-11-25
@jallvar

Decision. Forgot to mention that this form sends files
enctype="multipart/form-data">

P
pLavrenov, 2020-11-26
@pLavrenov

Attachments itself should be an array, but nullable should be removed.
Use the dump() and dd() functions to check what is coming into the controller. Most likely the form was sent incorrectly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question