duanguoping2016 2018-04-16 16:01
浏览 113

Laravel Validator从数组中删除空字段

I'm writing a test case to simulate a creation of a product. When creating the product, I'm passing an array of formats, and for some reasons, when validating with the controller, the thumbnail key is missing from the data.

This is the data I'm sending:

"formats" => array:1 [
  0 => array:3 [
    "upc" => "584979099857"
    "vcode" => "VX43V14FN910479274209"
    "thumbnail" => null
  ]
]

And I send it like the following in my test case:

$response = $this->post(route('products.store'), $product);

For some reasons, when sending the request, the thumbnail => null disappears from the formats attribute.

Therefore, when validating the request, I always get the error that the thumbnail has to be present.

request()->validate([
    'formats' => 'bail|required|array',
    'formats.*.upc' => 'bail|required|string|max:255',
    'formats.*.vcode' => 'bail|required|string|max:255',
    'formats.*.thumbnail' => 'bail|present|image'
]);

I've also tried changing the rules of thumbnail to:

bail|required|nullable|image

Or

bail|nullable|required|image

But then I get the required error.

Does anyone know why the thumbnail key is getting removed from the formats array upon sending the request?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记