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 ?

4

2 Answers 2

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.',
        ];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

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! :)
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.',

Comments

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.