我希望验证两个字段,即“类型”和“选项”,其中“类型”字段是枚举类型。只有当“类型”字段的值为“opt”时,才应验证“选项”字段。
$this->validate($request, [
'type' => 'required|in:opt,number,text,file,image',
'options'=>the condition I need(if type is 'opt')
]);
我希望验证两个字段,即“类型”和“选项”,其中“类型”字段是枚举类型。只有当“类型”字段的值为“opt”时,才应验证“选项”字段。
$this->validate($request, [
'type' => 'required|in:opt,number,text,file,image',
'options'=>the condition I need(if type is 'opt')
]);
在Laravel中,您可以使用required_if验证。
$this->validate($request, [
'type' => 'required|in:opt,number,text,file,image',
'options'=> 'required_if:type,==,opt'
]);
Here is a Documentation link
$this->validate($request, [
'type' => 'required|in:opt,number,text,file,image',
'options'=>($input['type'] == 'opt')?'required':''
]);