From 24a48b2fa6154b2ba2e669999e73a060f9e82080 Mon Sep 17 00:00:00 2001 From: Pusparaj Date: Fri, 23 Dec 2022 09:38:18 +0545 Subject: [PATCH] Update `decimal` validation rule to allow validation of signed numbers --- .../Validation/Concerns/ValidatesAttributes.php | 2 +- tests/Validation/ValidationValidatorTest.php | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index dc4dc56f4662..4d7df068b900 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -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)); diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 1cda44e88684..429141801e46 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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']); @@ -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()); @@ -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']);