【发布时间】:2021-12-08 20:33:06
【问题描述】:
我有一个格式的请求
array:2 [▼
"_token" => "Lqn3XPvbdhLC8iqs461xSrGYPmxmSv4PGCqH7LJQ"
"branch" => array:16 [▼
"customer_company_id" => "5"
"is_main" => "true"
]
]
我需要通过以下方式验证表单:
- 如果 customer_company_id 存在
name则不需要 - 如果 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