【问题标题】:Using required_without on Nested form在嵌套表单上使用 required_without
【发布时间】:2021-12-08 20:33:06
【问题描述】:

我有一个格式的请求

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

【问题讨论】:

  • 我很好奇您在哪里找到了这种验证的记录。在文档中验证数组需要使用点符号,例如'branch.is_main' => ..., 'branch.customer_company_id' => .... 但从您的消息来看,这似乎也有效(尽管我会尝试仅使用 name 而不是 branch.name

标签: laravel laravel-8 laravel-validation


【解决方案1】:

这个怎么样?

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

【讨论】:

    猜你喜欢
    • 2020-09-28
    • 1970-01-01
    • 2015-03-13
    • 2013-12-11
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多