我想用laravel做一张收据。我有26个输入字段。很难用条件逐个声明验证。是否可以要求至少1个字段?请帮忙。
$rules = [
'a_field' => 'required_without_all:b_field,c_field',
'b_field' => 'required_without_all:a_field,c_field',
'c_field' => 'required_without_all:a_field,b_field',
];
$message = [
'a_field' => 'The a is required when none of other fields are present.',
'b_field' => 'The b is required when none of other fields are present.',
'c_field' => 'The c is required when none of other fields are present.',
];
对于太多的输入
$fields = collect(['a', 'b', 'c', 'd', 'e']);
$rules = $fields->mapWithKeys(function ($field) use ($fields) {
return [
$field => 'required_without_all:' . $fields->reject(function ($item) use ($field) {
return $item == $field;
})->implode(',')
];
})->toArray();