微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将文件数据传递给 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 举报,一经查实,本站将立刻删除。