Skip to content

Commit ee0063f

Browse files
Fixed validation rule digits does not support int. (#2358)
* Fix strict_types=1, digits rule preg_match parameter 2 to be string error * Fixed validation rule `digits` does not support `int`. Co-authored-by: 李铭昕 <[email protected]>
1 parent 9476ca9 commit ee0063f

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/Concerns/ValidatesAttributes.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ public function validateDigits(string $attribute, $value, array $parameters): bo
301301
{
302302
$this->requireParameterCount(1, $parameters, 'digits');
303303

304+
$value = (string) $value;
304305
return ! preg_match('/[^0-9]/', $value)
305-
&& strlen((string) $value) == $parameters[0];
306+
&& strlen($value) == $parameters[0];
306307
}
307308

308309
/**
@@ -314,7 +315,8 @@ public function validateDigitsBetween(string $attribute, $value, array $paramete
314315
{
315316
$this->requireParameterCount(2, $parameters, 'digits_between');
316317

317-
$length = strlen((string) $value);
318+
$value = (string) $value;
319+
$length = strlen($value);
318320

319321
return ! preg_match('/[^0-9]/', $value)
320322
&& $length >= $parameters[0] && $length <= $parameters[1];

tests/Cases/ValidationValidatorTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,9 @@ public function testValidateDigits()
14691469
$v = new Validator($trans, ['foo' => '123'], ['foo' => 'Digits:200']);
14701470
$this->assertFalse($v->passes());
14711471

1472+
$v = new Validator($trans, ['foo' => 123], ['foo' => 'Digits:200']);
1473+
$this->assertFalse($v->passes());
1474+
14721475
$v = new Validator($trans, ['foo' => '+2.37'], ['foo' => 'Digits:5']);
14731476
$this->assertTrue($v->fails());
14741477

@@ -1482,6 +1485,9 @@ public function testValidateDigits()
14821485
$v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'digits_between:1,10']);
14831486
$this->assertFalse($v->passes());
14841487

1488+
$v = new Validator($trans, ['foo' => 123], ['foo' => 'digits_between:4,5']);
1489+
$this->assertFalse($v->passes());
1490+
14851491
$v = new Validator($trans, ['foo' => '123'], ['foo' => 'digits_between:4,5']);
14861492
$this->assertFalse($v->passes());
14871493

0 commit comments

Comments
 (0)