Laravel 验证规则 required_if 另一个字段值在数组中,无效
Laravel validation rule required_if another field value is in array, not working
假设我有表单向导,我想根据向导索引分离验证规则,假设我想有以下验证规则如果wizard_index
值为first
或all
或者如果 wizard_index
不存在。
$rules = [
'wizard_index' => ['required', 'string', 'in:first,second,third,all'],
'name' => ['required_if:wizard_index,in:first,all', 'string', 'max:50', 'min:3'],
'about' => ['required_if:wizard_index,in:first,all', 'string', 'max:500', 'min:10'],
'size' => [
'required_if:wizard_index,in:first,last', 'string',
'in:0 - 1,2 - 10,11 - 50,51 - 200,201 - 500,"501 - 1,000","1,001 - 5,000","5,001 + more"'
]
];
以上验证规则不起作用,但如果我删除 in:
并仅检查一个值,它就起作用了。
最后一点是我想从 wizard_index
中删除 required
规则,并为其他字段添加一个额外的条件,如果 wizard_index
不存在则应该需要这些条件。
只需单独添加这些规则,如下所示:
$rules = [
'wizard_index' => ['nullable', 'string', 'in:first,second,third,all'],
'name' => ['required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string', 'max:50', 'min:3'],
'about' => ['required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string', 'max:500', 'min:10'],
'size' => [
'required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string',
'in:0 - 1,2 - 10,11 - 50,51 - 200,201 - 500,"501 - 1,000","1,001 - 5,000","5,001 + more"'
]
];
假设我有表单向导,我想根据向导索引分离验证规则,假设我想有以下验证规则如果wizard_index
值为first
或all
或者如果 wizard_index
不存在。
$rules = [
'wizard_index' => ['required', 'string', 'in:first,second,third,all'],
'name' => ['required_if:wizard_index,in:first,all', 'string', 'max:50', 'min:3'],
'about' => ['required_if:wizard_index,in:first,all', 'string', 'max:500', 'min:10'],
'size' => [
'required_if:wizard_index,in:first,last', 'string',
'in:0 - 1,2 - 10,11 - 50,51 - 200,201 - 500,"501 - 1,000","1,001 - 5,000","5,001 + more"'
]
];
以上验证规则不起作用,但如果我删除 in:
并仅检查一个值,它就起作用了。
最后一点是我想从 wizard_index
中删除 required
规则,并为其他字段添加一个额外的条件,如果 wizard_index
不存在则应该需要这些条件。
只需单独添加这些规则,如下所示:
$rules = [
'wizard_index' => ['nullable', 'string', 'in:first,second,third,all'],
'name' => ['required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string', 'max:50', 'min:3'],
'about' => ['required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string', 'max:500', 'min:10'],
'size' => [
'required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string',
'in:0 - 1,2 - 10,11 - 50,51 - 200,201 - 500,"501 - 1,000","1,001 - 5,000","5,001 + more"'
]
];