Skip to content

Commit

Permalink
Update decimal validation rule to allow validation of signed numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Pusparaj committed Dec 23, 2022
1 parent e3546de commit 24a48b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public function validateDecimal($attribute, $value, $parameters)

$matches = [];

preg_match('/^\d*.(\d*)$/', $value, $matches);
preg_match('/^[+-]?\d*.(\d*)$/', $value, $matches);

$decimals = strlen(end($matches));

Expand Down
9 changes: 6 additions & 3 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2457,10 +2457,10 @@ public function testValidateDecimal()
$v = new Validator($trans, ['foo' => '1.2345'], ['foo' => 'Decimal:2,3']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => '1.234'], ['foo' => 'Decimal:2,3']);
$v = new Validator($trans, ['foo' => '-1.234'], ['foo' => 'Decimal:2,3']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:2,3']);
$v = new Validator($trans, ['foo' => '+1.23'], ['foo' => 'Decimal:2,3']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:2,3']);
Expand All @@ -2469,6 +2469,9 @@ public function testValidateDecimal()
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:2']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '-1.23'], ['foo' => 'Decimal:2']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '1.233'], ['foo' => 'Decimal:2']);
$this->assertFalse($v->passes());

Expand All @@ -2478,7 +2481,7 @@ public function testValidateDecimal()
$v = new Validator($trans, ['foo' => '1'], ['foo' => 'Decimal:0,1']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:0,1']);
$v = new Validator($trans, ['foo' => '-1.2'], ['foo' => 'Decimal:0,1']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:0,1']);
Expand Down

0 comments on commit 24a48b2

Please sign in to comment.