laravel 5.4 MIME 的自定义错误消息不起作用?

laravel 5.4 custom error message for MIME not working?

亲爱的,

我想为上传的图片制作自定义错误消息,

这是我的控制器代码:

protected function validator($data = [], $validateContent = []){
    $messages = [
        'imgUserAvatar:mimes' => 'File Type must be in jpeg,jpg,png'
    ];

    return Validator::make($data, $validateContent, $messages);
}
$validateContent = [
            'imgUserAvatar'     => 'mimes:jpeg,jpg,png|max:2000',
            'textUserEmail'     => 'required|unique:admin_user,user_email|max:50',
            'textFirstName'     => 'required|max:50',
            'textLastName'      => 'max:50',
            'textPwd'           => 'required|min:6|regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/',
            'textPwdConfrim'    => 'required|same:textPwd'
        ];

        $validation = $this->validator($request->all(), $validateContent);

但是这条自定义消息不想显示,显示的是默认消息Image Link

是不是我写错了什么?

你需要在字段和规则之间使用dot,所以你应该使用:

$messages = [
    'imgUserAvatar.mimes' => 'File Type must be in jpeg,jpg,png'
];

而不是

$messages = [
    'imgUserAvatar:mimes' => 'File Type must be in jpeg,jpg,png'
];