Skip to content

Commit

Permalink
[9.x] feature: min_digits and max_digits validation (#43797)
Browse files Browse the repository at this point in the history
* feature: Min/max digits validation

* rules

* message replacements

* fix inconsistent assertion

* fix failing test
  • Loading branch information
danharrin authored Aug 21, 2022
1 parent 93ed7f4 commit b8db0d2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ protected function replaceMin($message, $attribute, $rule, $parameters)
return str_replace(':min', $parameters[0], $message);
}

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

/**
* Replace all place-holders for the max rule.
*
Expand All @@ -140,6 +154,20 @@ protected function replaceMax($message, $attribute, $rule, $parameters)
return str_replace(':max', $parameters[0], $message);
}

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

/**
* Replace all place-holders for the multiple_of rule.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,23 @@ public function validateMax($attribute, $value, $parameters)
return $this->getSize($attribute, $value) <= $parameters[0];
}

/**
* Validate that an attribute has a maximum number of digits.
*
* @param string $attribute
* @param mixed $value
* @param array<int, int|string> $parameters
* @return bool
*/
public function validateMaxDigits($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'max');

$length = strlen((string) $value);

return ! preg_match('/[^0-9]/', $value) && $length <= $parameters[0];
}

/**
* Validate the guessed extension of a file upload is in a set of file extensions.
*
Expand Down Expand Up @@ -1410,6 +1427,23 @@ public function validateMin($attribute, $value, $parameters)
return $this->getSize($attribute, $value) >= $parameters[0];
}

/**
* Validate that an attribute has a minimum number of digits.
*
* @param string $attribute
* @param mixed $value
* @param array<int, int|string> $parameters
* @return bool
*/
public function validateMinDigits($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'min');

$length = strlen((string) $value);

return ! preg_match('/[^0-9]/', $value) && $length >= $parameters[0];
}

/**
* Validate the value of an attribute is a multiple of a given value.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,32 @@ public function testValidateDigits()

$v = new Validator($trans, ['foo' => '+12.3'], ['foo' => 'digits_between:1,6']);
$this->assertFalse($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => '12345'], ['foo' => 'min_digits:1']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'min_digits:1']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => '123'], ['foo' => 'min_digits:4']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => '+12.3'], ['foo' => 'min_digits:1']);
$this->assertFalse($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => '12345'], ['foo' => 'max_digits:6']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => 'bar'], ['foo' => 'max_digits:10']);
$this->assertFalse($v->passes());

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

$v = new Validator($trans, ['foo' => '+12.3'], ['foo' => 'max_digits:6']);
$this->assertFalse($v->passes());
}

public function testValidateSize()
Expand Down

0 comments on commit b8db0d2

Please sign in to comment.