Skip to content

Commit

Permalink
Added comparison-to-field methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpustulka committed Jan 4, 2018
1 parent c58a1e1 commit 95b48c6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/Validation/Validator.php
Expand Up @@ -1008,6 +1008,86 @@ public function notSameAs($field, $secondField, $message = null, $when = null)
]);
}

/**
* Add a rule to compare one field is greater than another.
*
* @param mixed $field The field you want to apply the rule to.
* @param mixed $secondField The field you want to compare against.
* @param string|null $message The error message when the rule fails.
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
* true when the validation rule should be applied.
* @see \Cake\Validation\Validation::compareFields()
* @return $this
*/
public function greaterThanField($field, $secondField, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'greaterThanField', $extra + [
'rule' => ['compareFields', $secondField, '>']
]);
}

/**
* Add a rule to compare one field is greater than or equal to another.
*
* @param mixed $field The field you want to apply the rule to.
* @param mixed $secondField The field you want to compare against.
* @param string|null $message The error message when the rule fails.
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
* true when the validation rule should be applied.
* @see \Cake\Validation\Validation::compareFields()
* @return $this
*/
public function greaterThanOrEqualToField($field, $secondField, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'greaterThanOrEqualToField', $extra + [
'rule' => ['compareFields', $secondField, '>=']
]);
}

/**
* Add a rule to compare one field is less than another.
*
* @param mixed $field The field you want to apply the rule to.
* @param mixed $secondField The field you want to compare against.
* @param string|null $message The error message when the rule fails.
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
* true when the validation rule should be applied.
* @see \Cake\Validation\Validation::compareFields()
* @return $this
*/
public function lessThanField($field, $secondField, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'lessThanField', $extra + [
'rule' => ['compareFields', $secondField, '<']
]);
}

/**
* Add a rule to compare one field is less than or equal to another.
*
* @param mixed $field The field you want to apply the rule to.
* @param mixed $secondField The field you want to compare against.
* @param string|null $message The error message when the rule fails.
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
* true when the validation rule should be applied.
* @see \Cake\Validation\Validation::compareFields()
* @return $this
*/
public function lessThanOrEqualToField($field, $secondField, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'lessThanOrEqualToField', $extra + [
'rule' => ['compareFields', $secondField, '<=']
]);
}

/**
* Add a rule to check if a field contains non alpha numeric characters.
*
Expand Down
50 changes: 50 additions & 0 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -1441,6 +1441,56 @@ public function testNotSameAs()
$this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
}

/**
* Tests the greaterThanField proxy method
*
* @return void
*/
public function testGreaterThanField()
{
$validator = new Validator();
$this->assertProxyMethod($validator, 'greaterThanField', 'other', ['other', '>'], 'compareFields');
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
}

/**
* Tests the greaterThanOrEqualToField proxy method
*
* @return void
*/
public function testGreaterThanOrEqualToField()
{
$validator = new Validator();
$this->assertProxyMethod($validator, 'greaterThanOrEqualToField', 'other', ['other', '>='], 'compareFields');
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
}

/**
* Tests the lessThanField proxy method
*
* @return void
*/
public function testLessThanField()
{
$validator = new Validator();
$this->assertProxyMethod($validator, 'lessThanField', 'other', ['other', '<'], 'compareFields');
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
$this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
}

/**
* Tests the lessThanOrEqualToField proxy method
*
* @return void
*/
public function testLessThanOrEqualToField()
{
$validator = new Validator();
$this->assertProxyMethod($validator, 'lessThanOrEqualToField', 'other', ['other', '<='], 'compareFields');
$this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
}

/**
* Tests the containsNonAlphaNumeric proxy method
*
Expand Down

0 comments on commit 95b48c6

Please sign in to comment.