0

I want to show the custom validation error message if user upload an image size of more than 4 MB. However, once submitted with an image of like above 4MB, it shows the default error message : "The file name failed to upload.". Below is my code in the controller:

$messages = [
            'fileName' => 'Image maximum size exceed. ',
];

$validator = Validator::make($request->all(), [
            'fileName' => 'max:4096', 
        ], $messages);

if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors());
        }

Here is the HTML code in the blade file:

<input type="file" name="fileName">
6
  • 2
    use fileName.max in your $messages array Commented Jul 17, 2018 at 12:01
  • @rkj done as 'fileName.max' => 'Image maximum size exceed. ', ---- but the same message showing. Commented Jul 17, 2018 at 12:03
  • did you set max_file_size value higher in php.ini file ? Commented Jul 17, 2018 at 12:04
  • Maybe this? resources/lang/en/validation.php 'custom' => [ 'attribute-name' => [ 'fileName' => 'custom-message', ], ], Commented Jul 17, 2018 at 12:08
  • @Bas I want to do the custm message by writing in the controller Commented Jul 17, 2018 at 12:12

2 Answers 2

1

I know this is an old question but I have to post this answer here. This was what worked for me

$messages = [
            'fileName.uploaded' => 'Image maximum size exceed. ',
];
Sign up to request clarification or add additional context in comments.

Comments

0

I used this line:

return redirect()->back()->withErrors($validator->customMessages);

and it solved the problem.

1 Comment

This doesn't work if I need to validate for multiple rules, it only returns the customMessage

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.