From 6b3d7635240011337230178e3d61134d7a02e4af Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 4 Aug 2023 10:25:34 +1000 Subject: [PATCH 1/2] improve decimal shape validation --- .../Concerns/ValidatesAttributes.php | 4 +++- tests/Validation/ValidationValidatorTest.php | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index aebd8f1195fd..7c2746b46472 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -593,7 +593,9 @@ public function validateDecimal($attribute, $value, $parameters) $matches = []; - preg_match('/^[+-]?\d*.(\d*)$/', $value, $matches); + if (preg_match('/^[+-]?\d*\.?(\d*)$/', $value, $matches) !== 1) { + return false; + } $decimals = strlen(end($matches)); diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index b199ac52ee5c..360b530750b3 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2860,6 +2860,26 @@ public function testValidateDecimal() $v = new Validator($trans, ['foo' => '1.88888888888888888888'], ['foo' => 'Decimal:20|Max:1.88888888888888888888']); $this->assertTrue($v->passes()); + + $v = new Validator($trans, [ + // these are the same number + 'decimal' => '0.555', + 'scientific' => '5.55e-1', + ], [ + 'decimal' => 'Decimal:0,2', + 'scientific' => 'Decimal:0,2', + ]); + $this->assertSame(['decimal', 'scientific'], $v->errors()->keys()); + + $v = new Validator($trans, [ + // these are the same number + 'decimal' => '0.555', + 'scientific' => '5.55e-1', + ], [ + 'decimal' => 'Decimal:0,3', + 'scientific' => 'Decimal:0,3', + ]); + $this->assertSame(['scientific'], $v->errors()->keys()); } public function testValidateInt() From 16441e583c8b945c11009e6e31f27203669b6a88 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Fri, 4 Aug 2023 10:52:32 +1000 Subject: [PATCH 2/2] Additional tests --- tests/Validation/ValidationValidatorTest.php | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 360b530750b3..5239dcf57995 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2880,6 +2880,30 @@ public function testValidateDecimal() 'scientific' => 'Decimal:0,3', ]); $this->assertSame(['scientific'], $v->errors()->keys()); + + $v = new Validator($trans, ['foo' => '+'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->fails()); + $v = new Validator($trans, ['foo' => '-'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->fails()); + $v = new Validator($trans, ['foo' => '10@12'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->fails()); + + $v = new Validator($trans, ['foo' => '+123'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); + $v = new Validator($trans, ['foo' => '-123'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); + $v = new Validator($trans, ['foo' => '+123.'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); + $v = new Validator($trans, ['foo' => '-123.'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); + $v = new Validator($trans, ['foo' => '123.'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); + $v = new Validator($trans, ['foo' => '123.'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); + $v = new Validator($trans, ['foo' => '123.34'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); + $v = new Validator($trans, ['foo' => '123.34'], ['foo' => 'Decimal:0,2']); + $this->assertTrue($v->passes()); } public function testValidateInt()