Skip to content

Commit 95b48c6

Browse files
Added comparison-to-field methods.
1 parent c58a1e1 commit 95b48c6

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/Validation/Validator.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,86 @@ public function notSameAs($field, $secondField, $message = null, $when = null)
10081008
]);
10091009
}
10101010

1011+
/**
1012+
* Add a rule to compare one field is greater than another.
1013+
*
1014+
* @param mixed $field The field you want to apply the rule to.
1015+
* @param mixed $secondField The field you want to compare against.
1016+
* @param string|null $message The error message when the rule fails.
1017+
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
1018+
* true when the validation rule should be applied.
1019+
* @see \Cake\Validation\Validation::compareFields()
1020+
* @return $this
1021+
*/
1022+
public function greaterThanField($field, $secondField, $message = null, $when = null)
1023+
{
1024+
$extra = array_filter(['on' => $when, 'message' => $message]);
1025+
1026+
return $this->add($field, 'greaterThanField', $extra + [
1027+
'rule' => ['compareFields', $secondField, '>']
1028+
]);
1029+
}
1030+
1031+
/**
1032+
* Add a rule to compare one field is greater than or equal to another.
1033+
*
1034+
* @param mixed $field The field you want to apply the rule to.
1035+
* @param mixed $secondField The field you want to compare against.
1036+
* @param string|null $message The error message when the rule fails.
1037+
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
1038+
* true when the validation rule should be applied.
1039+
* @see \Cake\Validation\Validation::compareFields()
1040+
* @return $this
1041+
*/
1042+
public function greaterThanOrEqualToField($field, $secondField, $message = null, $when = null)
1043+
{
1044+
$extra = array_filter(['on' => $when, 'message' => $message]);
1045+
1046+
return $this->add($field, 'greaterThanOrEqualToField', $extra + [
1047+
'rule' => ['compareFields', $secondField, '>=']
1048+
]);
1049+
}
1050+
1051+
/**
1052+
* Add a rule to compare one field is less than another.
1053+
*
1054+
* @param mixed $field The field you want to apply the rule to.
1055+
* @param mixed $secondField The field you want to compare against.
1056+
* @param string|null $message The error message when the rule fails.
1057+
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
1058+
* true when the validation rule should be applied.
1059+
* @see \Cake\Validation\Validation::compareFields()
1060+
* @return $this
1061+
*/
1062+
public function lessThanField($field, $secondField, $message = null, $when = null)
1063+
{
1064+
$extra = array_filter(['on' => $when, 'message' => $message]);
1065+
1066+
return $this->add($field, 'lessThanField', $extra + [
1067+
'rule' => ['compareFields', $secondField, '<']
1068+
]);
1069+
}
1070+
1071+
/**
1072+
* Add a rule to compare one field is less than or equal to another.
1073+
*
1074+
* @param mixed $field The field you want to apply the rule to.
1075+
* @param mixed $secondField The field you want to compare against.
1076+
* @param string|null $message The error message when the rule fails.
1077+
* @param string|callable|null $when Either 'create' or 'update' or a callable that returns
1078+
* true when the validation rule should be applied.
1079+
* @see \Cake\Validation\Validation::compareFields()
1080+
* @return $this
1081+
*/
1082+
public function lessThanOrEqualToField($field, $secondField, $message = null, $when = null)
1083+
{
1084+
$extra = array_filter(['on' => $when, 'message' => $message]);
1085+
1086+
return $this->add($field, 'lessThanOrEqualToField', $extra + [
1087+
'rule' => ['compareFields', $secondField, '<=']
1088+
]);
1089+
}
1090+
10111091
/**
10121092
* Add a rule to check if a field contains non alpha numeric characters.
10131093
*

tests/TestCase/Validation/ValidatorTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,56 @@ public function testNotSameAs()
14411441
$this->assertNotEmpty($validator->errors(['username' => 'foo', 'other' => 'foo']));
14421442
}
14431443

1444+
/**
1445+
* Tests the greaterThanField proxy method
1446+
*
1447+
* @return void
1448+
*/
1449+
public function testGreaterThanField()
1450+
{
1451+
$validator = new Validator();
1452+
$this->assertProxyMethod($validator, 'greaterThanField', 'other', ['other', '>'], 'compareFields');
1453+
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
1454+
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
1455+
}
1456+
1457+
/**
1458+
* Tests the greaterThanOrEqualToField proxy method
1459+
*
1460+
* @return void
1461+
*/
1462+
public function testGreaterThanOrEqualToField()
1463+
{
1464+
$validator = new Validator();
1465+
$this->assertProxyMethod($validator, 'greaterThanOrEqualToField', 'other', ['other', '>='], 'compareFields');
1466+
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 2]));
1467+
}
1468+
1469+
/**
1470+
* Tests the lessThanField proxy method
1471+
*
1472+
* @return void
1473+
*/
1474+
public function testLessThanField()
1475+
{
1476+
$validator = new Validator();
1477+
$this->assertProxyMethod($validator, 'lessThanField', 'other', ['other', '<'], 'compareFields');
1478+
$this->assertNotEmpty($validator->errors(['username' => 1, 'other' => 1]));
1479+
$this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
1480+
}
1481+
1482+
/**
1483+
* Tests the lessThanOrEqualToField proxy method
1484+
*
1485+
* @return void
1486+
*/
1487+
public function testLessThanOrEqualToField()
1488+
{
1489+
$validator = new Validator();
1490+
$this->assertProxyMethod($validator, 'lessThanOrEqualToField', 'other', ['other', '<='], 'compareFields');
1491+
$this->assertNotEmpty($validator->errors(['username' => 2, 'other' => 1]));
1492+
}
1493+
14441494
/**
14451495
* Tests the containsNonAlphaNumeric proxy method
14461496
*

0 commit comments

Comments
 (0)