0

我从输入类型=“文件”中获取文件,然后我有一个自定义验证类和表单请求类来验证文件数据。问题是我无法将文件数据传递给该类。

这是我的自定义验证类:

class ExcelTemplateValidator
{
    /**
     * Validate excel template
     * @return bool
     */
    public function validate($file)
    {
        dd($file); //I'm trying to debug data in here, it's only a string "file"
        $path1 = $file->store('temp'); 
        $path=storage_path('app').'/'.$path1; 

        $headerRow = (new HeadingRowImport)->toArray($path);
        $templateRow = [
            0 => [
                0 => [
                    0  => "phone",
                    1  => "code_1",
                    2  => "code_2",
                    3  => "code_3",
                    4  => "code_4",
                    5  => "code_5",
                    6  => "code_6",
                    7  => "code_7",
                    8  => "code_8",
                    9  => "code_9",
                    10 => "code_10"
                ]
            ]
        ];

        return $headerRow == $templateRow;
    }
}

在我的表单请求类中:

class FileRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'file' => 'required|valid'
        ];
    }

    /**
     * Customize message
     *
     * @return array
     */
    public function messages()
    {
        return [
            'file.required' => __('File is required'),
            'file.valid' => __('Template not valid'),
        ];
    }
}

最后,我在我的控制器函数中使用了这个表单请求类:

public function handleImportExcel(FileRequest $request)
{
    $file = $request->file;
    $excelData = Excel::toArray(new CampaignImport(), $file); //this work fine
}

我在验证类中的字符串上调用成员函数 store() 时出错,就像我在自定义验证类中评论一样,当我 dd() 我的 $file 变量时,它只有一个字符串“文件”。

我该如何解决这个问题?非常感谢!

4

0 回答 0