Laravel 至少需要一个字段
Laravel require at least one field
我正在构建表单。有 3 个特定的文本字段,用户至少应填写其中一个。我如何使用 Laravel 验证规则来实现它?
//dd($request)
array:6 [▼
"_token" => "o5td5RMv2EQ5mA7LqXpyMXCKIu7L78BfrRSEU1se"
"skill_id" => "6"
"plan_id" => "1"
//at least one of below fields should be filled
"context" => ""
"link" => ""
"desired_date" => ""
]
您可以使用:
required_without_all:foo,bar,...
在此,仅当所有其他指定字段都不存在时,验证字段才必须存在。
$rules = array(
'skill_id' => 'required_without_all:plan_id,context,link',
'plan_id' => 'required_without_all:skill_id,context,link',
);
或
您可以使用 required_unless
规则:https://laravel.com/docs/5.2/validation#rule-required-unless
required_unless:另一个字段,值,...
在这种情况下,验证字段必须存在,除非另一个字段等于任何值。
您可以使用required_without_all
$rules = array(
'context' => 'required_without_all:link,desired_date',
'link' => 'required_without_all:context,desired_date',
'desired_date' => 'required_without_all:context,link',
);
我正在构建表单。有 3 个特定的文本字段,用户至少应填写其中一个。我如何使用 Laravel 验证规则来实现它?
//dd($request)
array:6 [▼
"_token" => "o5td5RMv2EQ5mA7LqXpyMXCKIu7L78BfrRSEU1se"
"skill_id" => "6"
"plan_id" => "1"
//at least one of below fields should be filled
"context" => ""
"link" => ""
"desired_date" => ""
]
您可以使用:
required_without_all:foo,bar,...
在此,仅当所有其他指定字段都不存在时,验证字段才必须存在。
$rules = array(
'skill_id' => 'required_without_all:plan_id,context,link',
'plan_id' => 'required_without_all:skill_id,context,link',
);
或
您可以使用 required_unless
规则:https://laravel.com/docs/5.2/validation#rule-required-unless
required_unless:另一个字段,值,...
在这种情况下,验证字段必须存在,除非另一个字段等于任何值。
您可以使用required_without_all
$rules = array(
'context' => 'required_without_all:link,desired_date',
'link' => 'required_without_all:context,desired_date',
'desired_date' => 'required_without_all:context,link',
);