Laravel 5.4验证器没有看到输入字段

时间:2017-11-13 09:07:50

标签: php laravel laravel-5.4

所以我尝试使用Validator外观设置验证。出于某些奇怪的原因,验证器虽然在请求中存在但未看到其中一个输入字段。这是验证码:

        $rules = [
            'name'          => 'required',
            'resource_type' => 'required',
            'semester'      => 'required',
            'file'          => 'required|max:8196'
        ];
        $messages = [
            'name.required' => 'The name field is required',
            'semester.required' => 'The semester field is required',
            'resource_type.required' => 'The resource_type field is required',
            'file.required' => 'The file field is required',
            'file.max' => 'The max file size is 8196 kilobytes',
        ];

        $validator = Validator::make($request->input(), $rules, $messages);
        if ($validator->fails()) {
            return response()->json($validator->messages(), 422);
        }

这就是我正在进行的测试:

$file = UploadedFile::fake()->create('file.pdf', 512);
    $response = $this->json( "POST", 'api/v2/papers', [
        "name"          => $paper->name,
        "semester"      => $paper->semester,
        "resource_type" => $paper->resource_type,
        "file"          => $file,
    ] )->assertSuccessful();

当我打印$response->content()的值时,我得到了

{"file":["The file field is required"]}

文件字段存在,所以我不确定究竟发生了什么?

提前致谢。

1 个答案:

答案 0 :(得分:5)

不要使用$request->input(),它不包含文件。

使用$request->all()