Skip to content

Commit 24a48b2

Browse files
author
Pusparaj
committed
Update decimal validation rule to allow validation of signed numbers
1 parent e3546de commit 24a48b2

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ public function validateDecimal($attribute, $value, $parameters)
572572

573573
$matches = [];
574574

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

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

tests/Validation/ValidationValidatorTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,10 +2457,10 @@ public function testValidateDecimal()
24572457
$v = new Validator($trans, ['foo' => '1.2345'], ['foo' => 'Decimal:2,3']);
24582458
$this->assertFalse($v->passes());
24592459

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

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

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

2472+
$v = new Validator($trans, ['foo' => '-1.23'], ['foo' => 'Decimal:2']);
2473+
$this->assertTrue($v->passes());
2474+
24722475
$v = new Validator($trans, ['foo' => '1.233'], ['foo' => 'Decimal:2']);
24732476
$this->assertFalse($v->passes());
24742477

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

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

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

0 commit comments

Comments
 (0)