Browse Source

Add notSameAs() validation rule.

Robert Pustułka 8 years ago
parent
commit
b949ffc0c4
2 changed files with 36 additions and 3 deletions
  1. 23 2
      src/Validation/Validator.php
  2. 13 1
      tests/TestCase/Validation/ValidatorTest.php

+ 23 - 2
src/Validation/Validator.php

@@ -971,7 +971,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      * @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)
@@ -979,7 +979,28 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
         $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]
         ]);
     }
 

+ 13 - 1
tests/TestCase/Validation/ValidatorTest.php

@@ -1425,11 +1425,23 @@ class ValidatorTest extends TestCase
     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
      *
      * @return void