Skip to content

Commit

Permalink
[8.x] Fix multiple dots for digits_between rule (#42330)
Browse files Browse the repository at this point in the history
* Fix multiple dots for digits_between rule

* wip

* wip

* wip

* wip

* Update ValidatesAttributes.php

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
driesvints and taylorotwell authored May 10, 2022
1 parent abd5b38 commit 476f920
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,15 @@ public function validateDigitsBetween($attribute, $value, $parameters)

$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)
&& $length >= $parameters[0] && $length <= $parameters[1];
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,24 @@ public function testValidateDigits()

$v = new Validator($trans, ['foo' => '0.9876'], ['foo' => 'digits_between:1,5']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['foo' => '1..2'], ['foo' => 'digits_between:1,10']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['foo' => '123.456.789'], ['foo' => 'digits_between:1,10']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['foo' => '...'], ['foo' => 'digits_between:1,10']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['foo' => '.'], ['foo' => 'digits_between:1,10']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['foo' => '.2'], ['foo' => 'digits_between:0,10']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '2.'], ['foo' => 'digits_between:1,10']);
$this->assertTrue($v->passes());
}

public function testValidateSize()
Expand Down

0 comments on commit 476f920

Please sign in to comment.