Browse Source

Add Validation::hexColor()

chinpei215 9 years ago
parent
commit
55a39b52c0

+ 11 - 0
src/Validation/Validation.php

@@ -1431,6 +1431,17 @@ class Validation
     }
 
     /**
+     * Check that the input value is a 6 digits hex color.
+     *
+     * @param string|array $check The value to check
+     * @return bool Success
+     */
+    public static function hexColor($check)
+    {
+        return static::_check($check, '/^#[0-9a-f]{6}$/iD');
+    }
+
+    /**
      * Converts an array representing a date or datetime into a ISO string.
      * The arrays are typically sent for validation from a form generated by
      * the CakePHP FormHelper.

+ 19 - 0
src/Validation/Validator.php

@@ -1574,6 +1574,25 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
     }
 
     /**
+     * Add a validation rule to ensure a field is a 6 digits hex color value.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @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::color()
+     * @return $this
+     */
+    public function hexColor($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+
+        return $this->add($field, 'hexColor', $extra + [
+            'rule' => 'hexColor',
+        ]);
+    }
+
+    /**
      * Add a validation rule for a multiple select. Comparison is case sensitive by default.
      *
      * @param string $field The field you want to apply the rule to.

+ 14 - 0
tests/TestCase/Validation/ValidationTest.php

@@ -3103,4 +3103,18 @@ class ValidationTest extends TestCase
         $this->assertFalse(Validation::imageWidth($upload, '>', 2000));
         $this->assertFalse(Validation::imageWidth($upload, '==', 3000));
     }
+
+    /**
+     * Test hexColor
+     */
+    public function testHexColor()
+    {
+        $this->assertTrue(Validation::hexColor('#F01234'));
+        $this->assertTrue(Validation::hexColor('#F56789'));
+        $this->assertTrue(Validation::hexColor('#abcdef'));
+        $this->assertTrue(Validation::hexColor('#ABCDEF'));
+
+        $this->assertFalse(Validation::hexColor('#fff'));
+        $this->assertFalse(Validation::hexColor('ffffff'));
+    }
 }

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

@@ -1799,6 +1799,19 @@ class ValidatorTest extends TestCase
     }
 
     /**
+     * Tests the hexColor proxy method
+     *
+     * @return void
+     */
+    public function testHexColor()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'hexColor');
+        $this->assertEmpty($validator->errors(['username' => '#FFFFFF']));
+        $this->assertNotEmpty($validator->errors(['username' => 'FFFFFF']));
+    }
+
+    /**
      * Tests the multiple proxy method
      *
      * @return void