Skip to content

[12.x] New Validation Rules: even and odd #55284

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions src/Illuminate/Translation/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'doesnt_end_with' => 'The :attribute field must not end with one of the following: :values.',
'doesnt_start_with' => 'The :attribute field must not start with one of the following: :values.',
'email' => 'The :attribute field must be a valid email address.',
'even' => 'The :attribute field must be an even number.',
'ends_with' => 'The :attribute field must end with one of the following: :values.',
'enum' => 'The selected :attribute is invalid.',
'exists' => 'The selected :attribute is invalid.',
Expand Down Expand Up @@ -117,6 +118,7 @@
'not_in' => 'The selected :attribute is invalid.',
'not_regex' => 'The :attribute field format is invalid.',
'numeric' => 'The :attribute field must be a number.',
'odd' => 'The :attribute field must be an odd number.',
'password' => [
'letters' => 'The :attribute field must contain at least one letter.',
'mixed' => 'The :attribute field must contain at least one uppercase and one lowercase letter.',
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,30 @@ public function validateSize($attribute, $value, $parameters)
return BigNumber::of($this->getSize($attribute, $value))->isEqualTo($this->trim($parameters[0]));
}

/**
* Validate that a numeric attribute is even.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateEven($attribute, $value)
{
return $this->validateNumeric($attribute, $value) && BigNumber::of($this->trim($value))->isEven();
}

/**
* Validate that a numeric attribute is odd.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateOdd($attribute, $value)
{
return $this->validateNumeric($attribute, $value) && BigNumber::of($this->trim($value))->isOdd();
}

/**
* "Validate" optional attributes.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Validation/Rules/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,26 @@ public function exactly(int $value): Numeric
return $this->integer()->addRule('size:'.$value);
}

/**
* The field under validation must be an even number.
*
* @return $this
*/
public function even(): Numeric
{
return $this->addRule('even');
}

/**
* The field under validation must be an odd number.
*
* @return $this
*/
public function odd(): Numeric
{
return $this->addRule('odd');
}

/**
* Convert the rule to a validation string.
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/Validation/ValidationNumericRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,16 @@ public function testUniquenessValidation()
$rule = Rule::numeric()->integer()->digits(2)->exactly(2);
$this->assertEquals('numeric|integer|digits:2|size:2', (string) $rule);
}

public function testEvenRule()
{
$rule = Rule::numeric()->even();
$this->assertEquals('numeric|even', (string) $rule);
}

public function testOddRule()
{
$rule = Rule::numeric()->odd();
$this->assertEquals('numeric|odd', (string) $rule);
}
}
60 changes: 60 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9616,6 +9616,66 @@ public function testItTrimsSpaceFromParameters()
], $validator->messages()->keys());
}

public function testValidationPassesWithEvenNumber()
{
$v = new Validator(
$this->getIlluminateArrayTranslator(),
[
'number' => 2,
],
[
'number' => 'even',
]
);

$this->assertFalse($v->fails());
}

public function testValidationFailsWithEvenNumber()
{
$v = new Validator(
$this->getIlluminateArrayTranslator(),
[
'number' => 3,
],
[
'number' => 'even',
]
);

$this->assertTrue($v->fails());
}

public function testValidationPassWithOddNumber()
{
$v = new Validator(
$this->getIlluminateArrayTranslator(),
[
'number' => 3,
],
[
'number' => 'odd',
]
);

$this->assertFalse($v->fails());
}

public function testValidationFailsWithOddNumber()
{
$v = new Validator(
$this->getIlluminateArrayTranslator(),
[
'number' => 4,
],
[
'number' => 'odd',
]
);

$this->assertTrue($v->fails());
}

#[DataProvider('outsideRangeExponents')]
public function testItLimitsLengthOfScientificNotationExponent($value)
{
Expand Down
Loading