Skip to content

Commit

Permalink
tweak behavior of decimal rule
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 19, 2022
1 parent 5e9cd3b commit e89b2b0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ protected function replaceDateFormat($message, $attribute, $rule, $parameters)
return str_replace(':format', $parameters[0], $message);
}

/**
* Replace all place-holders for the decimal rule.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array<int,int> $parameters
* @return string
*/
protected function replaceDecimal($message, $attribute, $rule, $parameters)
{
return str_replace(
':decimal',
isset($parameters[1])
? $parameters[0].'-'.$parameters[1]
: $parameters[0],
$message
);
}

/**
* Replace all place-holders for the different rule.
*
Expand Down
6 changes: 5 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,12 @@ public function validateDecimal($attribute, $value, $parameters)

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

if (! isset($parameters[1])) {
return $decimals == $parameters[0];
}

return $decimals >= $parameters[0] &&
(! isset($parameters[1]) || $decimals <= $parameters[1]);
$decimals <= $parameters[1];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2466,12 +2466,12 @@ public function testValidateDecimal()
$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:2,3']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => '1.234'], ['foo' => 'Decimal:2']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Decimal:2']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => '1.233'], ['foo' => 'Decimal:2']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => '1.2'], ['foo' => 'Decimal:2']);
$this->assertFalse($v->passes());

Expand Down

0 comments on commit e89b2b0

Please sign in to comment.