dongyu9263 2019-03-27 11:59
浏览 52

表格验证和文件上传验证是否分开但在同一方法中使用?

When a form is submitted and it passes its validation which is defined in the ValidateTicketData extends FormRequest-Class you get to a site where there is displayed your form inputs.

There's also an <input type="file" /> to upload a file.
The Validation rules for this is defined in the ValidateUploadedFile extends FormRequest-Class.

When the file passes the validation, there should be displayed something like "file has been uploaded successfully" or so.

Here's the Controller-method:

public function store(ValidateFormData $request, ValidateUploadedFile $requestFile, $param = '')
{
  $validated = $request->validated();
  $formData = $request->all();

  if ($requestFile->hasFile('file')) {
     $file = $requestFile->file('file');
     if ($file->validated()) {
        $filename = uniqid() . $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
        $file->storeAs('store', $filename);
        $file->move(public_path('store') . $filename);
     }
  }
  return view('/validation', compact('formData'));
}

I was thinking about something like this.
If I leave out the part of the $requestFile... (so that the Controller would look like this:)

public function store(ValidateFormData $request, $param = '')
{
  $validated = $request->validated();
  $formData = $request->all();

  return view('/validation', compact('formData'));
}

... it works just fine.
The 'validation'-Site is displayed with the validated form inputs.

I somehow think this has something to do with the second Request class $requestFile, but I really don't know how to do it.

Since the file can be validated only after the form data is already validated I need two seperate Request classes..

I hope someone can help me with this.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么