如何将数组大小限制为包含2项或4项。而不是最小值2和最大值4。
$validity = Validator::make($data, [
'array',
// 'between:2,4', this will include 3 too. but I need only 2 and 4.
// 'in:2,4' something like this
]);
您需要一个自定义规则,可能是类似以下的闭包:
https://laravel.com/docs/7.x/validation#using-关闭
function ($attribute, $value, $fail) {
if (!in_array(count($value), [2, 4])) {
$fail($attribute.' must have either 2 elements or 4.');
}
}