Skip to content

Commit d487339

Browse files
authored
Fixed bug that the rule decimal cannot work well with size for validation.
1 parent 8d97efb commit d487339

File tree

6 files changed

+120
-6
lines changed

6 files changed

+120
-6
lines changed

publish/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'confirmed' => 'The :attribute confirmation does not match.',
4242
'date' => 'The :attribute is not a valid date.',
4343
'date_format' => 'The :attribute does not match the format :format.',
44+
'decimal' => 'The :attribute must have :decimal decimal places.',
4445
'different' => 'The :attribute and :other must be different.',
4546
'digits' => 'The :attribute must be :digits digits.',
4647
'digits_between' => 'The :attribute must be between :min and :max digits.',

publish/zh_CN/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'confirmed' => ':attribute 二次确认不匹配',
4242
'date' => ':attribute 必须是一个合法的日期',
4343
'date_format' => ':attribute 与给定的格式 :format 不符合',
44+
'decimal' => ':attribute 必须有 :decimal 位小数',
4445
'different' => ':attribute 必须不同于 :other',
4546
'digits' => ':attribute 必须是 :digits 位',
4647
'digits_between' => ':attribute 必须在 :min 和 :max 位之间',

src/Concerns/ReplacesAttributes.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ protected function replaceDateFormat(string $message, string $attribute, string
3131
return str_replace(':format', $parameters[0], $message);
3232
}
3333

34+
/**
35+
* Replace all place-holders for the decimal rule.
36+
*/
37+
protected function replaceDecimal(string $message, string $attribute, string $rule, array $parameters): string
38+
{
39+
return str_replace(
40+
':decimal',
41+
isset($parameters[1])
42+
? $parameters[0] . '-' . $parameters[1]
43+
: $parameters[0],
44+
$message
45+
);
46+
}
47+
3448
/**
3549
* Replace all place-holders for the different rule.
3650
*/

src/Concerns/ValidatesAttributes.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public function validateDecimal(string $attribute, mixed $value, array $paramete
387387

388388
$matches = [];
389389

390-
if (preg_match('/^[+-]?\d*\.?(\d*)$/', $value, $matches) !== 1) {
390+
if (preg_match('/^[+-]?\d*\.?(\d*)$/', (string) $value, $matches) !== 1) {
391391
return false;
392392
}
393393

@@ -935,7 +935,8 @@ public function validateMaxDigits(string $attribute, mixed $value, array $parame
935935
{
936936
$this->requireParameterCount(1, $parameters, 'max_digits');
937937

938-
$length = strlen((string) $value);
938+
$value = (string) $value;
939+
$length = strlen($value);
939940

940941
return ! preg_match('/[^0-9]/', $value) && $length <= $parameters[0];
941942
}
@@ -1011,7 +1012,8 @@ public function validateMinDigits(string $attribute, mixed $value, array $parame
10111012
{
10121013
$this->requireParameterCount(1, $parameters, 'min_digits');
10131014

1014-
$length = strlen((string) $value);
1015+
$value = (string) $value;
1016+
$length = strlen($value);
10151017

10161018
return ! preg_match('/[^0-9]/', $value) && $length >= $parameters[0];
10171019
}
@@ -1681,7 +1683,7 @@ protected function extractDistinctValues(string $attribute): array
16811683

16821684
$pattern = str_replace('\*', '[^.]+', preg_quote($attribute, '#'));
16831685

1684-
return Arr::where(Arr::dot($attributeData), fn ($value, $key) => (bool) preg_match('#^' . $pattern . '\z#u', $key));
1686+
return Arr::where(Arr::dot($attributeData), fn ($value, $key) => (bool) preg_match('#^' . $pattern . '\z#u', (string) $key));
16851687
}
16861688

16871689
/**
@@ -1725,7 +1727,7 @@ protected function getUniqueIds(array $parameters): array
17251727
*/
17261728
protected function prepareUniqueId($id)
17271729
{
1728-
if (preg_match('/\[(.*)\]/', $id, $matches)) {
1730+
if (preg_match('/\[(.*)\]/', (string) $id, $matches)) {
17291731
$id = $this->getValue($matches[1]);
17301732
}
17311733

src/Validator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Validator implements ValidatorContract
146146
/**
147147
* The numeric related validation rules.
148148
*/
149-
protected array $numericRules = ['Numeric', 'Integer'];
149+
protected array $numericRules = ['Numeric', 'Integer', 'Decimal'];
150150

151151
/**
152152
* @param TranslatorInterface $translator the Translator implementation

tests/Cases/ValidationValidatorTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,102 @@ public function testValidateInteger()
14551455
$this->assertTrue($v->passes());
14561456
}
14571457

1458+
public function testValidateDecimal()
1459+
{
1460+
$trans = $this->getIlluminateArrayTranslator();
1461+
$v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Decimal:2,3']);
1462+
$this->assertFalse($v->passes());
1463+
1464+
$v = new Validator($trans, ['foo' => '1.2345'], ['foo' => 'Decimal:2,3']);
1465+
$this->assertFalse($v->passes());
1466+
1467+
$v = new Validator($trans, ['foo' => '1.234'], ['foo' => 'Decimal:2,3']);
1468+
$this->assertTrue($v->passes());
1469+
1470+
$v = new Validator($trans, ['foo' => '-1.234'], ['foo' => 'Decimal:2,3']);
1471+
$this->assertTrue($v->passes());
1472+
1473+
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:2,3']);
1474+
$this->assertTrue($v->passes());
1475+
1476+
$v = new Validator($trans, ['foo' => '+1.23'], ['foo' => 'Decimal:2,3']);
1477+
$this->assertTrue($v->passes());
1478+
1479+
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:2,3']);
1480+
$this->assertFalse($v->passes());
1481+
1482+
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:2']);
1483+
$this->assertTrue($v->passes());
1484+
1485+
$v = new Validator($trans, ['foo' => '-1.23'], ['foo' => 'Decimal:2']);
1486+
$this->assertTrue($v->passes());
1487+
1488+
$v = new Validator($trans, ['foo' => '1.233'], ['foo' => 'Decimal:2']);
1489+
$this->assertFalse($v->passes());
1490+
1491+
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:2']);
1492+
$this->assertFalse($v->passes());
1493+
1494+
$v = new Validator($trans, ['foo' => '1'], ['foo' => 'Decimal:0,1']);
1495+
$this->assertTrue($v->passes());
1496+
1497+
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:0,1']);
1498+
$this->assertTrue($v->passes());
1499+
1500+
$v = new Validator($trans, ['foo' => '-1.2'], ['foo' => 'Decimal:0,1']);
1501+
$this->assertTrue($v->passes());
1502+
1503+
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:0,1']);
1504+
$this->assertFalse($v->passes());
1505+
1506+
$v = new Validator($trans, ['foo' => '1.8888888888'], ['foo' => 'Decimal:10']);
1507+
$this->assertTrue($v->passes());
1508+
1509+
$v = new Validator($trans, [
1510+
// these are the same number
1511+
'decimal' => '0.555',
1512+
'scientific' => '5.55e-1',
1513+
], [
1514+
'decimal' => 'Decimal:0,2',
1515+
'scientific' => 'Decimal:0,2',
1516+
]);
1517+
$this->assertSame(['decimal', 'scientific'], $v->errors()->keys());
1518+
1519+
$v = new Validator($trans, [
1520+
// these are the same number
1521+
'decimal' => '0.555',
1522+
'scientific' => '5.55e-1',
1523+
], [
1524+
'decimal' => 'Decimal:0,3',
1525+
'scientific' => 'Decimal:0,3',
1526+
]);
1527+
$this->assertSame(['scientific'], $v->errors()->keys());
1528+
1529+
$v = new Validator($trans, ['foo' => '+'], ['foo' => 'Decimal:0,2']);
1530+
$this->assertTrue($v->fails());
1531+
$v = new Validator($trans, ['foo' => '-'], ['foo' => 'Decimal:0,2']);
1532+
$this->assertTrue($v->fails());
1533+
$v = new Validator($trans, ['foo' => '10@12'], ['foo' => 'Decimal:0,2']);
1534+
$this->assertTrue($v->fails());
1535+
1536+
$v = new Validator($trans, ['foo' => '+123'], ['foo' => 'Decimal:0,2']);
1537+
$this->assertTrue($v->passes());
1538+
$v = new Validator($trans, ['foo' => '-123'], ['foo' => 'Decimal:0,2']);
1539+
$this->assertTrue($v->passes());
1540+
$v = new Validator($trans, ['foo' => '+123.'], ['foo' => 'Decimal:0,2']);
1541+
$this->assertTrue($v->passes());
1542+
$v = new Validator($trans, ['foo' => '-123.'], ['foo' => 'Decimal:0,2']);
1543+
$this->assertTrue($v->passes());
1544+
$v = new Validator($trans, ['foo' => '123.'], ['foo' => 'Decimal:0,2']);
1545+
$this->assertTrue($v->passes());
1546+
$v = new Validator($trans, ['foo' => '123.'], ['foo' => 'Decimal:0,2']);
1547+
$this->assertTrue($v->passes());
1548+
$v = new Validator($trans, ['foo' => '123.34'], ['foo' => 'Decimal:0,2']);
1549+
$this->assertTrue($v->passes());
1550+
$v = new Validator($trans, ['foo' => '123.34'], ['foo' => 'Decimal:0,2']);
1551+
$this->assertTrue($v->passes());
1552+
}
1553+
14581554
public function testValidateInt()
14591555
{
14601556
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)