Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] feature: min_digits and max_digits validation #43797

Merged
merged 5 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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