2
votes

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 ?

2
I don't want to use additional frameworks and libraries for image validation. I'm searching for solutions in the Laravel domain.Gevorg Melkumyan
@GufranHasan That's not an answer to the question, totally different setup.Douwe de Haan

2 Answers

8
votes

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.',
        ];
    }
1
votes

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.',