Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Improve decimal shape validation #47957

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
44 changes: 44 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down