Skip to content

Commit 57eca0c

Browse files
[9.x] Add decimal validation rule (#45356)
* Add `decimal` validation rule * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 2007b7f commit 57eca0c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,32 @@ public function validateDateEquals($attribute, $value, $parameters)
554554
return $this->compareDates($attribute, $value, $parameters, '=');
555555
}
556556

557+
/**
558+
* Validate that an attribute has a given number of decimal places.
559+
*
560+
* @param string $attribute
561+
* @param mixed $value
562+
* @param array<int, int|string> $parameters
563+
* @return bool
564+
*/
565+
public function validateDecimal($attribute, $value, $parameters)
566+
{
567+
if (!$this->validateNumeric($attribute, $value)) {
568+
return false;
569+
}
570+
571+
$this->requireParameterCount(1, $parameters, 'decimal');
572+
573+
$matches = [];
574+
575+
preg_match('/^\d*.(\d*)$/', $value, $matches);
576+
577+
$decimals = strlen(end($matches));
578+
579+
return $decimals >= $parameters[0] &&
580+
(! isset($parameters[1]) || $decimals <= $parameters[1]);
581+
}
582+
557583
/**
558584
* Validate that an attribute is different from another attribute.
559585
*

tests/Validation/ValidationValidatorTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,6 +2448,43 @@ public function testValidateInteger()
24482448
$this->assertTrue($v->passes());
24492449
}
24502450

2451+
public function testValidateDecimal()
2452+
{
2453+
$trans = $this->getIlluminateArrayTranslator();
2454+
$v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Decimal:2,3']);
2455+
$this->assertFalse($v->passes());
2456+
2457+
$v = new Validator($trans, ['foo' => '1.2345'], ['foo' => 'Decimal:2,3']);
2458+
$this->assertFalse($v->passes());
2459+
2460+
$v = new Validator($trans, ['foo' => '1.234'], ['foo' => 'Decimal:2,3']);
2461+
$this->assertTrue($v->passes());
2462+
2463+
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:2,3']);
2464+
$this->assertTrue($v->passes());
2465+
2466+
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:2,3']);
2467+
$this->assertFalse($v->passes());
2468+
2469+
$v = new Validator($trans, ['foo' => '1.234'], ['foo' => 'Decimal:2']);
2470+
$this->assertTrue($v->passes());
2471+
2472+
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:2']);
2473+
$this->assertTrue($v->passes());
2474+
2475+
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:2']);
2476+
$this->assertFalse($v->passes());
2477+
2478+
$v = new Validator($trans, ['foo' => '1'], ['foo' => 'Decimal:0,1']);
2479+
$this->assertTrue($v->passes());
2480+
2481+
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:0,1']);
2482+
$this->assertTrue($v->passes());
2483+
2484+
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:0,1']);
2485+
$this->assertFalse($v->passes());
2486+
}
2487+
24512488
public function testValidateInt()
24522489
{
24532490
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)