Skip to content

Commit f7dd856

Browse files
big-dreamliu21st
authored andcommitted
length 规则支持传入数组
验证长度在 0~255 之间:`'length' => [0, 255]`
1 parent 048c9fd commit f7dd856

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/Validate.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,8 +1688,8 @@ public function notBetween($value, $rule): bool
16881688

16891689
/**
16901690
* 验证数据长度
1691-
* @param mixed $value 字段值
1692-
* @param mixed $rule 验证规则
1691+
* @param mixed $value 字段值
1692+
* @param string|array|int|float $rule 验证规则
16931693
* @return bool
16941694
*/
16951695
public function length($value, $rule): bool
@@ -1702,7 +1702,10 @@ public function length($value, $rule): bool
17021702
$length = mb_strlen((string) $value);
17031703
}
17041704

1705-
if (is_string($rule) && str_contains($rule, ',')) {
1705+
if (is_array($rule)) {
1706+
// 长度区间
1707+
return $length >= $rule[0] && $length <= $rule[1];
1708+
} elseif (is_string($rule) && str_contains($rule, ',')) {
17061709
// 长度区间
17071710
[$min, $max] = explode(',', $rule);
17081711
return $length >= $min && $length <= $max;

0 commit comments

Comments
 (0)