在嵌套表单上使用 required_without

Using required_without on Nested form

我有一个格式为

的请求
array:2 [▼
  "_token" => "Lqn3XPvbdhLC8iqs461xSrGYPmxmSv4PGCqH7LJQ"
  "branch" => array:16 [▼
    "customer_company_id" => "5"
    "is_main" => "true"
  ]
]

我需要通过以下方式验证表单:

  1. 如果 customer_company_id 存在 name 则不需要
  2. 如果 customer_company_id 不存在,则需要 name

我的 FormRequest 看起来像

public function rules(): array
    {
        return [
            'branch' => [
                'is_main' => [
                    'required',
                    'boolean'
                ],
                'customer_company_id' => [
                    'required_without:branch.name'
                ],
            ],
        ];
}

它抛出以下错误

Method Illuminate\Validation\Validator::validateRequiredWithout:branch.name does not exist

这个怎么样?

return [
    'branch.name' => 'required_without:branch.customer_company_id',
    'branch.is_main' => 'required|boolean',
    'branch.customer_company_id' => 'required_without:branch.name',