From 75d317a19fe67f2ed7a2b88f30d49671b573a68f Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 7 Aug 2023 23:13:36 +1000 Subject: [PATCH] [9.x] Improve decimal shape validation (#47957) * improve decimal shape validation * Additional tests --- .../Concerns/ValidatesAttributes.php | 4 +- tests/Validation/ValidationValidatorTest.php | 44 +++++++++++++++++++ 2 files changed, 47 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..5239dcf57995 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2860,6 +2860,50 @@ 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()); + + $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()