将文件数据传递给 Laravel 中的自定义验证类

问题描述

我从我的 input type="file" 获取文件,然后我有一个自定义验证类和表单请求类来验证文件数据。问题是我无法将文件数据传递给那个类。

这是我的自定义验证类:

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 变量时,它只有一个字符串“文件”。>

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)