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

[5.6] Add new comparison validation rules #24091

Merged
merged 2 commits into from
May 3, 2018
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
108 changes: 108 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,98 @@ public function validateDifferent($attribute, $value, $parameters)
return true;
}

/**
* Validate that an attribute is greater than another attribute.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
public function validateGt($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'gt');

$comparedToValue = $this->getValue($parameters[0]);

if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
return $this->getSize($attribute, $value) > $parameters[0];
}

$this->requireSameTypeValues($value, $comparedToValue);

return $this->getSize($attribute, $value) > $this->getSize($attribute, $comparedToValue);
}

/**
* Validate that an attribute is less than another attribute.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
public function validateLt($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'lt');

$comparedToValue = $this->getValue($parameters[0]);

if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
return $this->getSize($attribute, $value) < $parameters[0];
}

$this->requireSameTypeValues($value, $comparedToValue);

return $this->getSize($attribute, $value) < $this->getSize($attribute, $comparedToValue);
}

/**
* Validate that an attribute is greater than or equal another attribute.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
public function validateGte($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'gte');

$comparedToValue = $this->getValue($parameters[0]);

if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
return $this->getSize($attribute, $value) >= $parameters[0];
}

$this->requireSameTypeValues($value, $comparedToValue);

return $this->getSize($attribute, $value) >= $this->getSize($attribute, $comparedToValue);
}

/**
* Validate that an attribute is less than or equal another attribute.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
public function validateLte($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'lte');

$comparedToValue = $this->getValue($parameters[0]);

if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
return $this->getSize($attribute, $value) <= $parameters[0];
}

$this->requireSameTypeValues($value, $comparedToValue);

return $this->getSize($attribute, $value) <= $this->getSize($attribute, $comparedToValue);
}

/**
* Validate that an attribute has a given number of digits.
*
Expand Down Expand Up @@ -1539,4 +1631,20 @@ protected function requireParameterCount($count, $parameters, $rule)
throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters.");
}
}

/**
* Require comparison values to be of the same type.
*
* @param mixed $first
* @param mixed $second
* @return void
*
* @throws \InvalidArgumentException
*/
protected function requireSameTypeValues($first, $second)
{
if (gettype($first) != gettype($second)) {
throw new InvalidArgumentException('The values under comparison must be of the same type');
}
}
}
4 changes: 2 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class Validator implements ValidatorContract
protected $dependentRules = [
'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different', 'Unique',
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual',
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
];

/**
Expand All @@ -182,7 +182,7 @@ class Validator implements ValidatorContract
*
* @var array
*/
protected $numericRules = ['Numeric', 'Integer'];
protected $numericRules = ['Numeric', 'Integer', 'Gt', 'Lt', 'Gte', 'Lte'];

/**
* Create a new Validator instance.
Expand Down
92 changes: 92 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,98 @@ public function testValidateDifferent()
$this->assertFalse($v->passes());
}

public function testGreaterThan()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'gt:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gt:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'gt:rhs']);
$this->assertTrue($v->fails());

$fileOne = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileOne->expects($this->any())->method('getSize')->will($this->returnValue(5472));
$fileTwo = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileTwo->expects($this->any())->method('getSize')->will($this->returnValue(3151));
$v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gt:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'gt:10']);
$this->assertTrue($v->passes());
}

public function testLessThan()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'lt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'lt:rhs']);
$this->assertTrue($v->passes());

$fileOne = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileOne->expects($this->any())->method('getSize')->will($this->returnValue(5472));
$fileTwo = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileTwo->expects($this->any())->method('getSize')->will($this->returnValue(3151));
$v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'lt:10']);
$this->assertTrue($v->fails());
}

public function testGreaterThanOrEqual()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'gte:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gte:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'gte:rhs']);
$this->assertTrue($v->fails());

$fileOne = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileOne->expects($this->any())->method('getSize')->will($this->returnValue(5472));
$fileTwo = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileTwo->expects($this->any())->method('getSize')->will($this->returnValue(5472));
$v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'gte:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'gte:15']);
$this->assertTrue($v->passes());
}

public function testLessThanOrEqual()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'lte:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lte:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => ['string'], 'rhs' => [1, 'string']], ['lhs' => 'lte:rhs']);
$this->assertTrue($v->passes());

$fileOne = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileOne->expects($this->any())->method('getSize')->will($this->returnValue(5472));
$fileTwo = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock();
$fileTwo->expects($this->any())->method('getSize')->will($this->returnValue(5472));
$v = new Validator($trans, ['lhs' => $fileOne, 'rhs' => $fileTwo], ['lhs' => 'lte:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'lte:10']);
$this->assertTrue($v->fails());
}

public function testValidateAccepted()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down