Skip to content

Commit

Permalink
Merge pull request #11278 from robertpustulka/3.next-not-same-as
Browse files Browse the repository at this point in the history
Add notSameAs validation rule.
  • Loading branch information
markstory committed Oct 9, 2017
2 parents cecff70 + d14c206 commit ddb31e8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 9 deletions.
21 changes: 20 additions & 1 deletion src/Validation/Validation.php
Expand Up @@ -308,12 +308,31 @@ public static function comparison($check1, $operator, $check2)
* @return bool
*/
public static function compareWith($check, $field, $context)
{
return self::compareFields($check, $field, true, $context);
}

/**
* Compare one field to another.
*
* Return true if the comparison matches the expected result.
*
* @param mixed $check The value to find in $field.
* @param string $field The field to check $check against. This field must be present in $context.
* @param bool $result The expected result of field comparison.
* @param array $context The validation context.
* @return bool
* @since 3.6.0
*/
public static function compareFields($check, $field, $result, $context)
{
if (!isset($context['data'][$field])) {
return false;
}

return $context['data'][$field] === $check;
$comparison = $context['data'][$field] === $check;

return $comparison === $result;
}

/**
Expand Down
25 changes: 23 additions & 2 deletions src/Validation/Validator.php
Expand Up @@ -971,15 +971,36 @@ public function notEquals($field, $value, $message = null, $when = null)
* @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::compareWith()
* @see \Cake\Validation\Validation::compareFields()
* @return $this
*/
public function sameAs($field, $secondField, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

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

/**
* Add a rule to compare that two fields have different values.
*
* @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
* @since 3.6.0
*/
public function notSameAs($field, $secondField, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

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

Expand Down
35 changes: 30 additions & 5 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -2741,28 +2741,53 @@ public function testUploadedFilePsr7($expected, $options)
}

/**
* Test the compareWith method.
* Test the compareFields method with true result.
*
* @return void
*/
public function testCompareWith()
public function testCompareFieldsTrue()
{
$context = [
'data' => [
'other' => 'a value'
]
];
$this->assertTrue(Validation::compareWith('a value', 'other', $context));
$this->assertTrue(Validation::compareFields('a value', 'other', true, $context));

$context = [
'data' => [
'other' => 'different'
]
];
$this->assertFalse(Validation::compareWith('a value', 'other', $context));
$this->assertFalse(Validation::compareFields('a value', 'other', true, $context));

$context = [];
$this->assertFalse(Validation::compareWith('a value', 'other', $context));
$this->assertFalse(Validation::compareFields('a value', 'other', true, $context));
}

/**
* Test the compareFields method with false result.
*
* @return void
*/
public function testCompareFieldsFalse()
{
$context = [
'data' => [
'other' => 'different'
]
];
$this->assertTrue(Validation::compareFields('a value', 'other', false, $context));

$context = [
'data' => [
'other' => 'a value'
]
];
$this->assertFalse(Validation::compareFields('a value', 'other', false, $context));

$context = [];
$this->assertFalse(Validation::compareFields('a value', 'other', false, $context));
}

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -1425,10 +1425,22 @@ public function testNotEquals()
public function testSameAs()
{
$validator = new Validator();
$this->assertProxyMethod($validator, 'sameAs', 'other', ['other'], 'compareWith');
$this->assertProxyMethod($validator, 'sameAs', 'other', ['other', true], 'compareFields');
$this->assertNotEmpty($validator->errors(['username' => 'foo']));
}

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

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

0 comments on commit ddb31e8

Please sign in to comment.