Skip to content

Commit 69af961

Browse files
authored
Consistency between digits and digits_between validation rules (#42358)
1 parent 5cd540e commit 69af961

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,18 @@ public function validateDigits($attribute, $value, $parameters)
564564
{
565565
$this->requireParameterCount(1, $parameters, 'digits');
566566

567-
return ! preg_match('/[^0-9]/', $value)
567+
$length = strlen((string) $value);
568+
569+
if (((string) $value) === '.') {
570+
return false;
571+
}
572+
573+
// Make sure there is not more than one dot...
574+
if (($length - strlen(str_replace('.', '', (string) $value))) > 1) {
575+
return false;
576+
}
577+
578+
return ! preg_match('/[^0-9.]/', $value)
568579
&& strlen((string) $value) == $parameters[0];
569580
}
570581

tests/Validation/ValidationValidatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,30 @@ public function testValidateDigits()
22722272
$v = new Validator($trans, ['foo' => '2e7'], ['foo' => 'Digits:3']);
22732273
$this->assertTrue($v->fails());
22742274

2275+
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'digits:3']);
2276+
$this->assertTrue($v->passes());
2277+
2278+
$v = new Validator($trans, ['foo' => '0.9876'], ['foo' => 'digits:5']);
2279+
$this->assertTrue($v->fails());
2280+
2281+
$v = new Validator($trans, ['foo' => '1..2'], ['foo' => 'digits:4']);
2282+
$this->assertTrue($v->fails());
2283+
2284+
$v = new Validator($trans, ['foo' => '123.456.789'], ['foo' => 'digits:10']);
2285+
$this->assertTrue($v->fails());
2286+
2287+
$v = new Validator($trans, ['foo' => '...'], ['foo' => 'digits:3']);
2288+
$this->assertTrue($v->fails());
2289+
2290+
$v = new Validator($trans, ['foo' => '.'], ['foo' => 'digits:1']);
2291+
$this->assertTrue($v->fails());
2292+
2293+
$v = new Validator($trans, ['foo' => '.2'], ['foo' => 'digits:2']);
2294+
$this->assertTrue($v->passes());
2295+
2296+
$v = new Validator($trans, ['foo' => '2.'], ['foo' => 'digits:2']);
2297+
$this->assertTrue($v->passes());
2298+
22752299
$trans = $this->getIlluminateArrayTranslator();
22762300
$v = new Validator($trans, ['foo' => '12345'], ['foo' => 'digits_between:1,6']);
22772301
$this->assertTrue($v->passes());

0 commit comments

Comments
 (0)