Laravel required_if 和哑剧无法处理文件输入

Laravel required_if and mimes not working on file input

请求文件中的代码

public function rules()
{
    return [
        'type' =>'required|in:1,2',
        'add' => 'required_if:type,2|in:1,2',
        'image' => 'required_if:add,2|mimes:jpeg,jpg,png,webp|max:10000'
    ];
}

添加属性 select 框有 2 个选项:

  1. 不要上传图片
  2. 上传图片

image 属性上我确实使用了 required_if add 属性的值为 2(即选项 2,我想添加image) 然后才将 image 字段设为必填字段,并进行 mime 和最大文件大小验证。

但问题是选项一是从添加 select 框中 select 编辑的,这意味着我不想上传图片,然后用户提交表单这会抛出验证:
图像必须是以下类型的文件:jpeg、jpg、png、webp。
根据我的验证代码,它应该忽略它,因为只有在选项2为select ed时才需要它,为什么此模仿验证错误会引发。

我认为您可以在 required_if

之后简单地添加 nullable
'cover'=>['required_if:can_upload_file,true','nullable','mimes:jpeg,jpg,png','max:5120']

添加此代码

 return [
    'image.*' => 'nullable',
    'image.required' => 'mimes:jpeg,jpg,png,webp|max:10000'
  ]