From c5acd1a8b8d857393b014d8bd4f7238f7b391885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=81nis=20L=C4=81cis?= Date: Thu, 12 May 2022 18:00:22 +0300 Subject: [PATCH] Consistency between digits and digits_between validation rules (#42358) --- .../Concerns/ValidatesAttributes.php | 13 +++++++++- tests/Validation/ValidationValidatorTest.php | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 28e61d36c461..0ca644f3e312 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -563,7 +563,18 @@ public function validateDigits($attribute, $value, $parameters) { $this->requireParameterCount(1, $parameters, 'digits'); - return ! preg_match('/[^0-9]/', $value) + $length = strlen((string) $value); + + if (((string) $value) === '.') { + return false; + } + + // Make sure there is not more than one dot... + if (($length - strlen(str_replace('.', '', (string) $value))) > 1) { + return false; + } + + return ! preg_match('/[^0-9.]/', $value) && strlen((string) $value) == $parameters[0]; } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 95ca15884d9a..61b07f45bb27 100644 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2506,6 +2506,30 @@ public function testValidateDigits() $v = new Validator($trans, ['foo' => '2e7'], ['foo' => 'Digits:3']); $this->assertTrue($v->fails()); + $v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'digits:3']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['foo' => '0.9876'], ['foo' => 'digits:5']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '1..2'], ['foo' => 'digits:4']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '123.456.789'], ['foo' => 'digits:10']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '...'], ['foo' => 'digits:3']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '.'], ['foo' => 'digits:1']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '.2'], ['foo' => 'digits:2']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['foo' => '2.'], ['foo' => 'digits:2']); + $this->assertTrue($v->passes()); + $trans = $this->getIlluminateArrayTranslator(); $v = new Validator($trans, ['foo' => '12345'], ['foo' => 'digits_between:1,6']); $this->assertTrue($v->passes());