Skip to content

Commit

Permalink
Consistency between digits and digits_between validation rules (larav…
Browse files Browse the repository at this point in the history
  • Loading branch information
taai authored and chu121su12 committed May 14, 2022
1 parent ce35a49 commit c5acd1a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
24 changes: 24 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit c5acd1a

Please sign in to comment.