Browse Source

Merge pull request #7941 from cakephp/validator-methods

Added helper methods in validator for the functions in Validation class
José Lorenzo Rodríguez 10 years ago
parent
commit
8543053ad4

+ 0 - 3
src/Validation/Validation.php

@@ -130,9 +130,6 @@ class Validation
      * Returns true if field is left blank -OR- only whitespace characters are present in its value
      * Whitespace characters include Space, Tab, Carriage Return, Newline
      *
-     * $check can be passed as an array:
-     * ['check' => 'valueToCheck'];
-     *
      * @param string|array $check Value to check
      * @return bool Success
      * @deprecated 3.0.2

+ 744 - 1
src/Validation/Validator.php

@@ -17,6 +17,7 @@ namespace Cake\Validation;
 use ArrayAccess;
 use ArrayIterator;
 use Countable;
+use InvalidArgumentException;
 use IteratorAggregate;
 
 /**
@@ -511,7 +512,7 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
      * method called will take precedence.
      *
      * @param string $field the name of the field
-     * @param string $message The validation message to show if the field is not
+     * @param string $message The message to show if the field is not
      * @param bool|string|callable $when  Indicates when the field is not allowed
      * to be empty. Valid values are true (always), 'create', 'update'. If a
      * callable is passed then the field will allowed be empty only when
@@ -536,6 +537,748 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
     }
 
     /**
+     * Add a notBlank rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::notBlank()
+     * @return $this
+     */
+    public function notBlank($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'notBlank', $extra + [
+            'rule' => 'notBlank',
+        ]);
+    }
+
+    /**
+     * Add an alphanumeric rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::alphaNumeric()
+     * @return $this
+     */
+    public function alphaNumeric($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'alphaNumeric', $extra + [
+            'rule' => 'alphaNumeric',
+        ]);
+    }
+
+    /**
+     * Add an rule that ensures a string length is within a range.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param array $range The inclusive minimum and maximum length you want permitted.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::alphaNumeric()
+     * @return $this
+     */
+    public function lengthBetween($field, array $range, $message = null, $when = null)
+    {
+        if (count($range) !== 2) {
+            throw new InvalidArgumentException('The $range argument requires 2 numbers');
+        }
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'lengthBetween', $extra + [
+            'rule' => ['lengthBetween', array_shift($range), array_shift($range)],
+        ]);
+    }
+
+    /**
+     * Add a credit card rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $type The type of cards you want to allow. Defaults to 'all'.
+     *   You can also supply an array of accepted card types. e.g `['mastercard', 'visa', 'amex']`
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::cc()
+     * @return $this
+     */
+    public function creditCard($field, $type = 'all', $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'creditCard', $extra + [
+            'rule' => ['cc', $type, true],
+        ]);
+    }
+
+    /**
+     * Add a greater than comparison rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int|float $value The value user data must be greater than.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::comparison()
+     * @return $this
+     */
+    public function greaterThan($field, $value, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'greaterThan', $extra + [
+            'rule' => ['comparison', '>', $value]
+        ]);
+    }
+
+    /**
+     * Add a greater than or equal to comparison rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int|float $value The value user data must be greater than or equal to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::comparison()
+     * @return $this
+     */
+    public function greaterThanOrEqual($field, $value, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'greaterThanOrEqual', $extra + [
+            'rule' => ['comparison', '>=', $value]
+        ]);
+    }
+
+    /**
+     * Add a less than comparison rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int|float $value The value user data must be less than.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::comparison()
+     * @return $this
+     */
+    public function lessThan($field, $value, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'lessThan', $extra + [
+            'rule' => ['comparison', '<', $value]
+        ]);
+    }
+
+    /**
+     * Add a less than or equal comparison rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int|float $value The value user data must be less than or equal to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::comparison()
+     * @return $this
+     */
+    public function lessThanOrEqual($field, $value, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'lessThanOrEqual', $extra + [
+            'rule' => ['comparison', '<=', $value]
+        ]);
+    }
+
+    /**
+     * Add a equal to comparison rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int|float $value The value user data must be equal to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::comparison()
+     * @return $this
+     */
+    public function equals($field, $value, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'equals', $extra + [
+            'rule' => ['comparison', '=', $value]
+        ]);
+    }
+
+    /**
+     * Add a not equal to comparison rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int|float $value The value user data must be not be equal to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::comparison()
+     * @return $this
+     */
+    public function notEquals($field, $value, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'notEquals', $extra + [
+            'rule' => ['comparison', '!=', $value]
+        ]);
+    }
+
+    /**
+     * Add a rule to compare two fields to each other.
+     *
+     * If both fields have the exact same value the rule will pass.
+     *
+     * @param mixed $field The field you want to apply the rule to.
+     * @param mixed $secondField The field you want to compare against.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::compareWith()
+     * @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]
+        ]);
+    }
+
+    /**
+     * Add a rule to check if a field contains non alpha numeric characters.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int $limit The minimum number of non-alphanumeric fields required.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::containsNonAlphaNumeric()
+     * @return $this
+     */
+    public function containsNonAlphaNumeric($field, $limit = 1, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'containsNonAlphaNumeric', $extra + [
+            'rule' => ['containsNonAlphaNumeric', $limit]
+        ]);
+    }
+
+    /**
+     * Add a date format validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param array $formats A list of accepted date formats.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::date()
+     * @return $this
+     */
+    public function date($field, $formats = ['ymd'], $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'date', $extra + [
+            'rule' => ['date', $formats]
+        ]);
+    }
+
+    /**
+     * Add a date time format validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param array $formats A list of accepted date formats.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::datetime()
+     * @return $this
+     */
+    public function dateTime($field, $formats = ['ymd'], $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'dateTime', $extra + [
+            'rule' => ['datetime', $formats]
+        ]);
+    }
+
+    /**
+     * Add a time format validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::time()
+     * @return $this
+     */
+    public function time($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'time', $extra + [
+            'rule' => 'time'
+        ]);
+    }
+
+    /**
+     * Add a boolean validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::boolean()
+     * @return $this
+     */
+    public function boolean($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'boolean', $extra + [
+            'rule' => 'boolean'
+        ]);
+    }
+
+    /**
+     * Add a decimal validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int $places The number of decimal places to require.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::decimal()
+     * @return $this
+     */
+    public function decimal($field, $places = null, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'decimal', $extra + [
+            'rule' => ['decimal', $places]
+        ]);
+    }
+
+    /**
+     * Add an email validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param bool $checkMX Whether or not to check the MX records.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::email()
+     * @return $this
+     */
+    public function email($field, $checkMX = false, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'email', $extra + [
+            'rule' => ['email', $checkMX]
+        ]);
+    }
+
+    /**
+     * Add an IP validation rule to a field.
+     *
+     * This rule will accept both IPv4 and IPv6 addresses.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::ip()
+     * @return $this
+     */
+    public function ip($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'ip', $extra + [
+            'rule' => 'ip'
+        ]);
+    }
+
+    /**
+     * Add an IPv4 validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::ip()
+     * @return $this
+     */
+    public function ipv4($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'ipv4', $extra + [
+            'rule' => ['ip', 'ipv4']
+        ]);
+    }
+
+    /**
+     * Add an IPv6 validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::ip()
+     * @return $this
+     */
+    public function ipv6($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'ipv6', $extra + [
+            'rule' => ['ip', 'ipv6']
+        ]);
+    }
+
+    /**
+     * Add a string length validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int $min The minimum length required.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::minLength()
+     * @return $this
+     */
+    public function minLength($field, $min, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'minLength', $extra + [
+            'rule' => ['minLength', $min]
+        ]);
+    }
+
+    /**
+     * Add a string length validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param int $max The maximum length allowed.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::maxLength()
+     * @return $this
+     */
+    public function maxLength($field, $max, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'maxLength', $extra + [
+            'rule' => ['maxLength', $max]
+        ]);
+    }
+
+    /**
+     * Add a numeric value validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::numeric()
+     * @return $this
+     */
+    public function numeric($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'numeric', $extra + [
+            'rule' => 'numeric'
+        ]);
+    }
+
+    /**
+     * Add a natural number validation rule to a field.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::naturalNumber()
+     * @return $this
+     */
+    public function naturalNumber($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'naturalNumber', $extra + [
+            'rule' => ['naturalNumber', false]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field is a non negative integer.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::naturalNumber()
+     * @return $this
+     */
+    public function nonNegativeInteger($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'nonNegativeInteger', $extra + [
+            'rule' => ['naturalNumber', true]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field is within a numeric range
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param array $range The inclusive upper and lower bounds of the valid range.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::range()
+     * @return $this
+     */
+    public function range($field, array $range, $message = null, $when = null)
+    {
+        if (count($range) !== 2) {
+            throw new InvalidArgumentException('The $range argument requires 2 numbers');
+        }
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'range', $extra + [
+            'rule' => ['range', array_shift($range), array_shift($range)]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field is a URL.
+     *
+     * This validator does not require a protocol.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::url()
+     * @return $this
+     */
+    public function url($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'url', $extra + [
+            'rule' => ['url', false]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field is a URL.
+     *
+     * This validator requires the URL to have a protocol.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::url()
+     * @return $this
+     */
+    public function urlWithProtocol($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'urlWithProtocol', $extra + [
+            'rule' => ['url', true]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure the field value is within a whitelist.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param array $list The list of valid options.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::inList()
+     * @return $this
+     */
+    public function inList($field, array $list, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'inList', $extra + [
+            'rule' => ['inList', $list]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure the field is a UUID
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::uuid()
+     * @return $this
+     */
+    public function uuid($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'uuid', $extra + [
+            'rule' => 'uuid'
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure the field is an uploaded file
+     *
+     * For options see Cake\Validation\Validation::uploadedFile()
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param array $options An array of options.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::uploadedFile()
+     * @return $this
+     */
+    public function uploadedFile($field, array $options, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'uploadedFile', $extra + [
+            'rule' => ['uploadedFile', $options]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure the field is a lat/long tuple.
+     *
+     * e.g. `<lat>, <lng>`
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::uuid()
+     * @return $this
+     */
+    public function latLong($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'latLong', $extra + [
+            'rule' => 'geoCoordinate'
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure the field is a latitude.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::latitude()
+     * @return $this
+     */
+    public function latitude($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'latitude', $extra + [
+            'rule' => 'latitude'
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure the field is a longitude.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::longitude()
+     * @return $this
+     */
+    public function longitude($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'longitude', $extra + [
+            'rule' => 'longitude'
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field contains only ascii bytes
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::ascii()
+     * @return $this
+     */
+    public function ascii($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'ascii', $extra + [
+            'rule' => 'ascii'
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field contains only BMP utf8 bytes
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::utf8()
+     * @return $this
+     */
+    public function utf8($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'utf8', $extra + [
+            'rule' => ['utf8', ['extended' => false]]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field contains only utf8 bytes.
+     *
+     * This rule will accept 3 and 4 byte UTF8 sequences, which are necessary for emoji.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::utf8()
+     * @return $this
+     */
+    public function utf8Extended($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'utf8Extended', $extra + [
+            'rule' => ['utf8', ['extended' => true]]
+        ]);
+    }
+
+    /**
+     * Add a validation rule to ensure a field is an integer value.
+     *
+     * @param string $field The field you want to apply the rule to.
+     * @param string $message The error message when the rule fails.
+     * @param string|callable $when Either 'create' or 'update' or a callable that returns
+     *   true when the valdiation rule should be applied.
+     * @see Cake\Validation\Validation::isInteger()
+     * @return $this
+     */
+    public function integer($field, $message = null, $when = null)
+    {
+        $extra = array_filter(['on' => $when, 'message' => $message]);
+        return $this->add($field, 'integer', $extra + [
+            'rule' => 'isInteger'
+        ]);
+    }
+
+    /**
      * Returns whether or not a field can be left empty for a new or already existing
      * record.
      *

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

@@ -1047,4 +1047,498 @@ class ValidatorTest extends TestCase
         $this->assertNotEmpty($validator->errors(['user' => [['username' => 'example']]], true));
         $this->assertEmpty($validator->errors(['user' => [['username' => 'a']]], false));
     }
+
+    /**
+     * Tests the notBlank proxy method
+     *
+     * @return void
+     */
+    public function testNotBlank()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'notBlank');
+        $this->assertNotEmpty($validator->errors(['username' => '  ']));
+    }
+
+    /**
+     * Tests the alphanumeric proxy method
+     *
+     * @return void
+     */
+    public function testAlphanumeric()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'alphaNumeric');
+        $this->assertNotEmpty($validator->errors(['username' => '$']));
+    }
+
+    /**
+     * Tests the lengthBetween proxy method
+     *
+     * @return void
+     */
+    public function testLengthBetween()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'lengthBetween', [5, 7], [5, 7]);
+        $this->assertNotEmpty($validator->errors(['username' => 'foo']));
+    }
+
+    /**
+     * Tests the lengthBetween proxy method
+     *
+     * @expectedException InvalidArgumentException
+     * @return void
+     */
+    public function testLengthBetweenFailure()
+    {
+        $validator = new Validator();
+        $validator->lengthBetween('username', [7]);
+    }
+
+    /**
+     * Tests the creditCard proxy method
+     *
+     * @return void
+     */
+    public function testCreditCard()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'creditCard', 'all', ['all', true], 'cc');
+        $this->assertNotEmpty($validator->errors(['username' => 'foo']));
+    }
+
+    /**
+     * Tests the greaterThan proxy method
+     *
+     * @return void
+     */
+    public function testGreaterThan()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'greaterThan', 5, ['>', 5], 'comparison');
+        $this->assertNotEmpty($validator->errors(['username' => 2]));
+    }
+
+    /**
+     * Tests the greaterThanOrEqual proxy method
+     *
+     * @return void
+     */
+    public function testGreaterThanOrEqual()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'greaterThanOrEqual', 5, ['>=', 5], 'comparison');
+        $this->assertNotEmpty($validator->errors(['username' => 2]));
+    }
+
+    /**
+     * Tests the lessThan proxy method
+     *
+     * @return void
+     */
+    public function testLessThan()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'lessThan', 5, ['<', 5], 'comparison');
+        $this->assertNotEmpty($validator->errors(['username' => 5]));
+    }
+
+    /**
+     * Tests the lessThanOrEqual proxy method
+     *
+     * @return void
+     */
+    public function testLessThanOrEqual()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'lessThanOrEqual', 5, ['<=', 5], 'comparison');
+        $this->assertNotEmpty($validator->errors(['username' => 6]));
+    }
+
+    /**
+     * Tests the equals proxy method
+     *
+     * @return void
+     */
+    public function testEquals()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'equals', 5, ['=', 5], 'comparison');
+        $this->assertNotEmpty($validator->errors(['username' => 6]));
+    }
+
+    /**
+     * Tests the notEquals proxy method
+     *
+     * @return void
+     */
+    public function testNotEquals()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'notEquals', 5, ['!=', 5], 'comparison');
+        $this->assertNotEmpty($validator->errors(['username' => 5]));
+    }
+
+    /**
+     * Tests the sameAs proxy method
+     *
+     * @return void
+     */
+    public function testSameAs()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'sameAs', 'other', ['other'], 'compareWith');
+        $this->assertNotEmpty($validator->errors(['username' => 'foo']));
+    }
+
+    /**
+     * Tests the containsNonAlphaNumeric proxy method
+     *
+     * @return void
+     */
+    public function testContainsNonAlphaNumeric()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'containsNonAlphaNumeric', 2, [2]);
+        $this->assertNotEmpty($validator->errors(['username' => '$']));
+    }
+
+    /**
+     * Tests the date proxy method
+     *
+     * @return void
+     */
+    public function testDate()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'date', ['ymd'], [['ymd']]);
+        $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
+    }
+
+
+    /**
+     * Tests the dateTime proxy method
+     *
+     * @return void
+     */
+    public function testDateTime()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'dateTime', ['ymd'], [['ymd']], 'datetime');
+        $this->assertNotEmpty($validator->errors(['username' => 'not a date']));
+    }
+
+    /**
+     * Tests the time proxy method
+     *
+     * @return void
+     */
+    public function testTime()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'time');
+        $this->assertNotEmpty($validator->errors(['username' => 'not a time']));
+    }
+
+    /**
+     * Tests the boolean proxy method
+     *
+     * @return void
+     */
+    public function testBoolean()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'boolean');
+        $this->assertNotEmpty($validator->errors(['username' => 'not a boolean']));
+    }
+
+    /**
+     * Tests the decimal proxy method
+     *
+     * @return void
+     */
+    public function testDecimal()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'decimal', 2, [2]);
+        $this->assertNotEmpty($validator->errors(['username' => 10.1]));
+    }
+
+    /**
+     * Tests the ip proxy methods
+     *
+     * @return void
+     */
+    public function testIps()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'ip');
+        $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
+
+
+        $this->assertProxyMethod($validator, 'ipv4', null, ['ipv4'], 'ip');
+        $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
+
+        $this->assertProxyMethod($validator, 'ipv6', null, ['ipv6'], 'ip');
+        $this->assertNotEmpty($validator->errors(['username' => 'not ip']));
+    }
+
+    /**
+     * Tests the minLength proxy method
+     *
+     * @return void
+     */
+    public function testMinLength()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'minLength', 2, [2]);
+        $this->assertNotEmpty($validator->errors(['username' => 'a']));
+    }
+
+    /**
+     * Tests the maxLength proxy method
+     *
+     * @return void
+     */
+    public function testMaxLength()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'maxLength', 2, [2]);
+        $this->assertNotEmpty($validator->errors(['username' => 'aaa']));
+    }
+
+    /**
+     * Tests the numeric proxy method
+     *
+     * @return void
+     */
+    public function testNumeric()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'numeric');
+        $this->assertEmpty($validator->errors(['username' => '22']));
+        $this->assertNotEmpty($validator->errors(['username' => 'a']));
+    }
+
+    /**
+     * Tests the naturalNumber proxy method
+     *
+     * @return void
+     */
+    public function testNaturalNumber()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'naturalNumber', null, [false]);
+        $this->assertNotEmpty($validator->errors(['username' => 0]));
+    }
+
+    /**
+     * Tests the nonNegativeInteger proxy method
+     *
+     * @return void
+     */
+    public function testNonNegativeInteger()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'nonNegativeInteger', null, [true], 'naturalNumber');
+        $this->assertNotEmpty($validator->errors(['username' => -1]));
+    }
+
+    /**
+     * Tests the range proxy method
+     *
+     * @return void
+     */
+    public function testRange()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'range', [1, 4], [1, 4]);
+        $this->assertNotEmpty($validator->errors(['username' => 5]));
+    }
+
+    /**
+     * Tests the range failure case
+     *
+     * @expectedException InvalidArgumentException
+     * @return void
+     */
+    public function testRangeFailure()
+    {
+        $validator = new Validator();
+        $validator->range('username', [1]);
+    }
+    /**
+     * Tests the url proxy method
+     *
+     * @return void
+     */
+    public function testUrl()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'url', null, [false]);
+        $this->assertNotEmpty($validator->errors(['username' => 'not url']));
+    }
+
+    /**
+     * Tests the urlWithProtocol proxy method
+     *
+     * @return void
+     */
+    public function testUrlWithProtocol()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'urlWithProtocol', null, [true], 'url');
+        $this->assertNotEmpty($validator->errors(['username' => 'google.com']));
+    }
+
+    /**
+     * Tests the inList proxy method
+     *
+     * @return void
+     */
+    public function testInList()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'inList', ['a', 'b'], [['a', 'b']]);
+        $this->assertNotEmpty($validator->errors(['username' => 'c']));
+    }
+
+    /**
+     * Tests the uuid proxy method
+     *
+     * @return void
+     */
+    public function testUuid()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'uuid');
+        $this->assertNotEmpty($validator->errors(['username' => 'not uuid']));
+    }
+
+    /**
+     * Tests the uploadedFile proxy method
+     *
+     * @return void
+     */
+    public function testUploadedFile()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'uploadedFile', ['foo' => 'bar'], [['foo' => 'bar']]);
+        $this->assertNotEmpty($validator->errors(['username' => []]));
+    }
+
+    /**
+     * Tests the latlog proxy methods
+     *
+     * @return void
+     */
+    public function testLatLong()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'latLong', null, [], 'geoCoordinate');
+        $this->assertNotEmpty($validator->errors(['username' => 2000]));
+
+        $this->assertProxyMethod($validator, 'latitude');
+        $this->assertNotEmpty($validator->errors(['username' => 2000]));
+
+        $this->assertProxyMethod($validator, 'longitude');
+        $this->assertNotEmpty($validator->errors(['username' => 2000]));
+    }
+
+    /**
+     * Tests the ascii proxy method
+     *
+     * @return void
+     */
+    public function testAscii()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'ascii');
+        $this->assertNotEmpty($validator->errors(['username' => 'ü']));
+    }
+
+    /**
+     * Tests the utf8 proxy methods
+     *
+     * @return void
+     */
+    public function testUtf8()
+    {
+        // Grinning face
+        $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
+        $validator = new Validator();
+
+        $this->assertProxyMethod($validator, 'utf8', null, [['extended' => false]]);
+        $this->assertEmpty($validator->errors(['username' => 'ü']));
+        $this->assertNotEmpty($validator->errors(['username' => $extended]));
+    }
+
+    /**
+     * Test utf8extended proxy method.
+     *
+     * @return void
+     */
+    public function testUtf8Extended()
+    {
+        // Grinning face
+        $extended = 'some' . "\xf0\x9f\x98\x80" . 'value';
+        $validator = new Validator();
+
+        $this->assertProxyMethod($validator, 'utf8Extended', null, [['extended' => true]], 'utf8');
+        $this->assertEmpty($validator->errors(['username' => 'ü']));
+        $this->assertEmpty($validator->errors(['username' => $extended]));
+    }
+
+    /**
+     * Tests the email proxy method
+     *
+     * @return void
+     */
+    public function testEmail()
+    {
+        $validator = new Validator();
+        $validator->email('username');
+        $this->assertEmpty($validator->errors(['username' => 'test@example.com']));
+        $this->assertNotEmpty($validator->errors(['username' => 'not an email']));
+    }
+
+    /**
+     * Tests the integer proxy method
+     *
+     * @return void
+     */
+    public function testInteger()
+    {
+        $validator = new Validator();
+        $this->assertProxyMethod($validator, 'integer', null, [], 'isInteger');
+        $this->assertNotEmpty($validator->errors(['username' => 'not integer']));
+    }
+
+    protected function assertProxyMethod($validator, $method, $extra = null, $pass = [], $name = null)
+    {
+        $name = $name ?: $method;
+        if ($extra !== null) {
+            $this->assertSame($validator, $validator->{$method}('username', $extra));
+        } else {
+            $this->assertSame($validator, $validator->{$method}('username'));
+        }
+
+        $rule = $validator->field('username')->rule($method);
+        $this->assertNull($rule->get('message'), 'Message is present when it should not be');
+        $this->assertNull($rule->get('on'), 'On clause is present when it should not be');
+        $this->assertEquals($name, $rule->get('rule'), 'Rule name does not match');
+        $this->assertEquals($pass, $rule->get('pass'), 'Passed options are different');
+        $this->assertEquals('default', $rule->get('provider'), 'Provider does not match');
+
+        if ($extra !== null) {
+            $validator->{$method}('username', $extra, 'the message', 'create');
+        } else {
+            $validator->{$method}('username', 'the message', 'create');
+        }
+
+        $rule = $validator->field('username')->rule($method);
+        $this->assertEquals('the message', $rule->get('message'), 'Error messages are not the same');
+        $this->assertEquals('create', $rule->get('on'), 'On clause is wrong');
+    }
 }