2

In my CustomFormRequest file I have the following rule for image file:

public function rules() {
        return [
            'image' => 'image|max:2047',
        ];
    }

and the appropriate validation messages:

public function messages() {
        return [
            'image.image' => 'The type of the uploaded file should be an image.',
            'image.max' => 'Failed to upload an image. The image maximum size is 2MB.',
        ];
    }

But the message for maximum size rule doesn't appear. The default message for max file size is appearing instead of it. What I'm doing wrong ?

Gevorg Melkumyan
  • 628
  • 1
  • 12
  • 24
  • Please see this solution https://stackoverflow.com/questions/27520121/how-to-pass-custom-validation-message-to-cviebrock-image-validator-in-laravel – Gufran Hasan Oct 11 '18 at 09:23
  • I don't want to use additional frameworks and libraries for image validation. I'm searching for solutions in the Laravel domain. – Gevorg Melkumyan Oct 11 '18 at 09:27
  • @GufranHasan That's not an answer to the question, totally different setup. – Douwe de Haan Oct 11 '18 at 09:28
  • Possible duplicate of [Custom error message for Laravel validation rule: Dimensions](https://stackoverflow.com/questions/47335447/custom-error-message-for-laravel-validation-rule-dimensions) – Lakhwinder Singh Oct 11 '18 at 10:05

2 Answers2

8

For couple hours of research I finally found the way:

public function messages() {
        return [
            'image.image' => 'The type of the uploaded file should be an image.',
            'image.uploaded' => 'Failed to upload an image. The image maximum size is 2MB.',
        ];
    }
Gevorg Melkumyan
  • 628
  • 1
  • 12
  • 24
  • 1
    Thank you @Gevrog My friend, You spent couple of hours but your this reply saved my couple of hours. Thank you again for your reply here. YOU SAVED MY DAY! :) – Asif Ali Jul 02 '21 at 19:10
1

https://stackoverflow.com/a/52762776/12809994 answer works for single errors, im gonna comment another way of setting this as a default error message for documenting purposes. To set a default message to appear instead of the default one you can go to a file called validation.php that has all default error messages, it is located in resources/lang/{language} and you can add the custom message there like below

'uploaded' => 'Failed to upload an file, the maximum size is 2MB.',