rkrite's avatar

How to get the exact error message from Laravel validate?

I have a view/form that allows the user to upload an image file.

My validate code in my controller says this...

    $this->validate($request, [
        'name' => 'required',
        'desc' => 'required',
        'active' => 'required',
        'image' => 'image|nullable|max:2000'
    ]);

When the user uploads an image larger than 2MB, it correctly fails validation, but the error presented to my view (in the $errors variable) is "The image failed to upload.". I would like to see something more along the lines of "The uploaded image must be smaller than 2MB".

Is this possible?

Thanks very much.

0 likes
9 replies
rkrite's avatar

further info....

Looking at the $errors variable, it shows the following...

ViewErrorBag {#444 ▼ #bags: array:1 [▼ "default" => MessageBag {#445 ▼ #messages: array:1 [▼ "image" => array:1 [▼ 0 => "The image failed to upload." ] ] #format: ":message" } ] }

So I can see that there is no further detail in the error message.

Thanks again.

rkrite's avatar

Thanks edoc. It seems that this isn't working for me. I'm still getting the same message :( If in fact it is a different error, is there a way to check it?

edoc's avatar

in fact it is a different error

what's the error message saying?

edoc's avatar

did you add enctype="multipart/form-data" to your form element?

rkrite's avatar

@EDOC - Yes. I am able to upload an image that is smaller than 2MB successfully. When I upload an image larger than my validation allows, it only gives me the error "The image failed to upload".

rkrite's avatar

@EDOC - You're a wizard edoc! That's the problem. Thanks so much :)

1 like

Please or to participate in this conversation.