未显示文件扩展名验证消息

File extension validation message not showing

我正在尝试验证文件扩展名。 and also this one

问题

在我的测试用例中,我尝试上传一个 zip 文件。错误信息是:分类图片上传失败。预期的错误消息是下面请求 class 中给出的错误消息。

我的代码如下。

请求Class

class UpdateCategoryRequest extends Request
{
    public function __construct() {
    }

    public function authorize() {
        return true;
    }

    public function wantsJson() {
        return true;
    }

    public function rules() {
        return [
            'Category_Image' => "image|mimes:bmp,png,jpg,gif"
        ];
    }

    public function messages() {
        return [
            "Category_Image.mimes" =>  
                           "Only image files with extension(bmp,png,jpg,gif) are allowed."
        ];
    }
}

JQueryAjax代码

var fileUpload = $("#Category_Image").get(0);
var files = fileUpload.files;

var fileData = new FormData();

for (var i = 0; i < files.length; i++) {
    fileData.append("Category_Image", files[i]);
}

$.ajax({
    method: "POST",
    url:    "{!! route('Update_Category') !!}",
    cache:  false,
    async:  true,
    data:   fileData,
    processData: false,
    contentType: false,
    success: function(result) {
    },
    error: function(result) {
    }
});

您的代码是正确的,您只是错过了将 [] 附加到 Category_image 名称,如下所示:

fileData.append("Category_Image[]", files[i]);