Laravel 中图像大小规则的自定义验证消息

Custom validation message for image size rule in Laravel

在我的 CustomFormRequest 文件中,我有以下图像文件规则:

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

和适当的验证信息:

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

但是最大尺寸规则的消息没有出现。出现最大文件大小的默认消息而不是它。我做错了什么?

经过几个小时的研究,我终于找到了方法:

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

答案适用于单个错误,我将评论另一种将其设置为默认错误消息的方式,用于记录目的。 要设置显示默认消息而不是默认消息,您可以转到名为 validation.php 的文件,该文件包含所有默认错误消息,它位于 resources/lang/{language} 中,您可以添加自定义留言如下

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