diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 48abfdf5db15..3c2f2bb6d12b 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -599,7 +599,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 89e769282c8d..b1e5da1315cb 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()