Skip to content

Commit 75d317a

Browse files
authored
[9.x] Improve decimal shape validation (#47957)
* improve decimal shape validation * Additional tests
1 parent 5366a90 commit 75d317a

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,9 @@ public function validateDecimal($attribute, $value, $parameters)
593593

594594
$matches = [];
595595

596-
preg_match('/^[+-]?\d*.(\d*)$/', $value, $matches);
596+
if (preg_match('/^[+-]?\d*\.?(\d*)$/', $value, $matches) !== 1) {
597+
return false;
598+
}
597599

598600
$decimals = strlen(end($matches));
599601

tests/Validation/ValidationValidatorTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,50 @@ public function testValidateDecimal()
28602860

28612861
$v = new Validator($trans, ['foo' => '1.88888888888888888888'], ['foo' => 'Decimal:20|Max:1.88888888888888888888']);
28622862
$this->assertTrue($v->passes());
2863+
2864+
$v = new Validator($trans, [
2865+
// these are the same number
2866+
'decimal' => '0.555',
2867+
'scientific' => '5.55e-1',
2868+
], [
2869+
'decimal' => 'Decimal:0,2',
2870+
'scientific' => 'Decimal:0,2',
2871+
]);
2872+
$this->assertSame(['decimal', 'scientific'], $v->errors()->keys());
2873+
2874+
$v = new Validator($trans, [
2875+
// these are the same number
2876+
'decimal' => '0.555',
2877+
'scientific' => '5.55e-1',
2878+
], [
2879+
'decimal' => 'Decimal:0,3',
2880+
'scientific' => 'Decimal:0,3',
2881+
]);
2882+
$this->assertSame(['scientific'], $v->errors()->keys());
2883+
2884+
$v = new Validator($trans, ['foo' => '+'], ['foo' => 'Decimal:0,2']);
2885+
$this->assertTrue($v->fails());
2886+
$v = new Validator($trans, ['foo' => '-'], ['foo' => 'Decimal:0,2']);
2887+
$this->assertTrue($v->fails());
2888+
$v = new Validator($trans, ['foo' => '10@12'], ['foo' => 'Decimal:0,2']);
2889+
$this->assertTrue($v->fails());
2890+
2891+
$v = new Validator($trans, ['foo' => '+123'], ['foo' => 'Decimal:0,2']);
2892+
$this->assertTrue($v->passes());
2893+
$v = new Validator($trans, ['foo' => '-123'], ['foo' => 'Decimal:0,2']);
2894+
$this->assertTrue($v->passes());
2895+
$v = new Validator($trans, ['foo' => '+123.'], ['foo' => 'Decimal:0,2']);
2896+
$this->assertTrue($v->passes());
2897+
$v = new Validator($trans, ['foo' => '-123.'], ['foo' => 'Decimal:0,2']);
2898+
$this->assertTrue($v->passes());
2899+
$v = new Validator($trans, ['foo' => '123.'], ['foo' => 'Decimal:0,2']);
2900+
$this->assertTrue($v->passes());
2901+
$v = new Validator($trans, ['foo' => '123.'], ['foo' => 'Decimal:0,2']);
2902+
$this->assertTrue($v->passes());
2903+
$v = new Validator($trans, ['foo' => '123.34'], ['foo' => 'Decimal:0,2']);
2904+
$this->assertTrue($v->passes());
2905+
$v = new Validator($trans, ['foo' => '123.34'], ['foo' => 'Decimal:0,2']);
2906+
$this->assertTrue($v->passes());
28632907
}
28642908

28652909
public function testValidateInt()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy