On this page
Class Validation
Validation Class. Used for validation of model data
Offers different validation methods.
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Utility/Validation.php
Properties summary
- 
    
    Some complex patterns needed in multiple placesarray
- 
    
    arrayHolds an array of errors messages set in this class. These are used for debugging purposes 
Method Summary
- Runs a regular expression match.
- Helper method that can be stubbed in testing.
- 
     Attempts to pass unhandled Validation locales to a class starting with $classPrefix and ending with Validation. For example $classPrefix = 'nl', the class would be NlValidation.
- Lazily populate the IP address patterns used for validations
- Reset internal variables for another validation run.
- Checks that a string contains only integer or letters
- Alias of Validator::lengthBetween() for backwards compatibility.
- 
     Returns true if field is left blank -OR- only whitespace characters are present in its value Whitespace characters include Space, Tab, Carriage Return, Newline 
- Boolean validation, determines if value passed is a boolean integer or true/false.
- 
     Validation of credit card numbers. Returns true if $check is in the proper credit card format. 
- Used to compare 2 numeric values.
- Used when a custom regular expression is needed.
- 
     Date validation, determines if the string passed is a valid date. keys that expect full month, day and year will validate leap years. 
- Validates a datetime value
- Checks that a value is a valid decimal. Both the sign and exponent are optional.
- Validates for an email address.
- Check that value is exactly $comparedTo.
- Check that value has a valid file extension.
- Checks the filesize
- Checks if a value is in a given list. Comparison is case sensitive by default.
- Validation of an IP address.
- 
     Checks that a string length is within s specified range. Spaces are included in the character count. Returns true is string matches value min, max, or between min and max, 
- Luhn algorithm
- Checks whether the length of a string (in characters) is smaller or equal to a maximal length..
- Checks whether the length of a string (in bytes) is smaller or equal to a maximal length..
- Checks the mime type of a file.
- Checks whether the length of a string (in characters) is greater or equal to a minimal length.
- Checks whether the length of a string (in bytes) is greater or equal to a minimal length.
- Checks that a value is a monetary amount.
- Validate a multiple select. Comparison is case sensitive by default.
- Checks if a value is a natural number.
- Checks that a string contains something other than whitespace
- Backwards compatibility wrapper for Validation::notBlank().
- Checks if a value is numeric.
- Check that a value is a valid phone number.
- Checks that a given value is a valid postal code.
- 
     Validate that a number is in specified range. if $lower and $upper are not set, will return true if $check is a legal finite on this platform 
- Checks that a value is a valid Social Security Number.
- 
     Time validation, determines if the string passed is a valid time. Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m) Does not allow/validate seconds. 
- Checking for upload errors
- Validate an uploaded file.
- Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt
- Runs an user-defined validation.
- Checks that a value is a valid UUID - http://tools.ietf.org/html/rfc4122
Method Detail
_check()source protected static
_check( string $check , string $regex )Runs a regular expression match.
Parameters
- 
     string $check
- Value to check against the $regex expression
- 
     string $regex
- Regular expression
Returns
booleanSuccess of match
_isUploadedFile()source protected static
_isUploadedFile( string $path )Helper method that can be stubbed in testing.
Parameters
- 
     string $path
- The path to check.
Returns
booleanWhether or not the file is an uploaded file.
_pass()source protected static
_pass( string $method , mixed $check , string $classPrefix )Attempts to pass unhandled Validation locales to a class starting with $classPrefix and ending with Validation. For example $classPrefix = 'nl', the class would be NlValidation.
Parameters
- 
     string $method
- The method to call on the other class.
- 
     mixed $check
- The value to check or an array of parameters for the method to be called.
- 
     string $classPrefix
- The prefix for the class to do the validation.
Returns
mixedReturn of Passed method, false on failure
_populateIp()source protected static
_populateIp( )Lazily populate the IP address patterns used for validations
alphaNumeric()source public static
alphaNumeric( string|array $check )Checks that a string contains only integer or letters
Returns true if string contains only integer or letters
$check can be passed as an array: array('check' => 'valueToCheck');
Parameters
- 
     string|array $check
- Value to check
Returns
booleanSuccess
between()source public static
between( string $check , integer $min , integer $max )Alias of Validator::lengthBetween() for backwards compatibility.
Deprecated
Deprecated 2.6. Use Validator::lengthBetween() instead.Parameters
- 
     string $check
- Value to check for length
- 
     integer $min
- Minimum value in range (inclusive)
- 
     integer $max
- Maximum value in range (inclusive)
Returns
booleanSuccess
See
Validator::lengthBetween()blank()source public static
blank( string|array $check )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: array('check' => 'valueToCheck');
Parameters
- 
     string|array $check
- Value to check
Returns
booleanSuccess
boolean()source public static
boolean( string $check )Boolean validation, determines if value passed is a boolean integer or true/false.
Parameters
- 
     string $check
- a valid boolean
Returns
booleanSuccess
cc()source public static
cc( string|array $check , string|array $type 'fast' , boolean $deep false , string $regex null )Validation of credit card numbers. Returns true if $check is in the proper credit card format.
Parameters
- 
     string|array $check
- credit card number to validate
- 
     string|array $typeoptional 'fast'
- 
     'all' may be passed as a sting, defaults to fast which checks format of most major credit cards if an array is used only the values of the array are checked. Example: array('amex', 'bankcard', 'maestro') 
- 
     boolean $deepoptional false
- set to true this will check the Luhn algorithm of the credit card.
- 
     string $regexoptional null
- A custom regex can also be passed, this will be used instead of the defined regex values
Returns
booleanSuccess
See
Validation::luhn()comparison()source public static
comparison( string|array $check1 , string $operator null , integer $check2 null )Used to compare 2 numeric values.
Parameters
- 
     string|array $check1
- 
     if string is passed for a string must also be passed for $check2 used as an array it must be passed as array('check1' => value, 'operator' => 'value', 'check2' -> value) 
- 
     string $operatoroptional null
- 
     Can be either a word or operand is greater >, is less <, greater or equal >= less or equal <=, is less <, equal to ==, not equal != 
- 
     integer $check2optional null
- only needed if $check1 is a string
Returns
booleanSuccess
custom()source public static
custom( string|array $check , string $regex null )Used when a custom regular expression is needed.
Parameters
- 
     string|array $check
- 
     When used as a string, $regex must also be a valid regular expression. As and array: array('check' => value, 'regex' => 'valid regular expression') 
- 
     string $regexoptional null
- If $check is passed as a string, $regex must also be set to valid regular expression
Returns
booleanSuccess
date()source public static
date( string $check , string|array $format 'ymd' , string $regex null )Date validation, determines if the string passed is a valid date. keys that expect full month, day and year will validate leap years.
Years are valid from 1800 to 2999.
Formats:
- dmy27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash
- mdy12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
- ymd2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
- dMy27 December 2006 or 27 Dec 2006
- MdyDecember 27, 2006 or Dec 27, 2006 comma is optional
- MyDecember 2006 or Dec 2006
- my12/2006 or 12/06 separators can be a space, period, dash, forward slash
- ym2006/12 or 06/12 separators can be a space, period, dash, forward slash
- y2006 just the year without any separators
Parameters
- 
     string $check
- a valid date string
- 
     string|array $formatoptional 'ymd'
- 
     Use a string or an array of the keys above. Arrays should be passed as array('dmy', 'mdy', etc) 
- 
     string $regexoptional null
- If a custom regular expression is used this is the only validation that will occur.
Returns
booleanSuccess
datetime()source public static
datetime( string $check , string|array $dateFormat 'ymd' , string $regex null )Validates a datetime value
All values matching the "date" core validation rule, and the "time" one will be valid
Parameters
- 
     string $check
- Value to check
- 
     string|array $dateFormatoptional 'ymd'
- Format of the date part. See Validation::date for more information.
- 
     string $regexoptional null
- Regex for the date part. If a custom regular expression is used this is the only validation that will occur.
Returns
booleanTrue if the value is valid, false otherwise
See
Validation::dateValidation::time
decimal()source public static
decimal( float $check , integer $places null , string $regex null )Checks that a value is a valid decimal. Both the sign and exponent are optional.
Valid Places:
- null => Any number of decimal places, including none. The '.' is not required.
- true => Any number of decimal places greater than 0, or a float|double. The '.' is required.
- 1..N => Exactly that many number of decimal places. The '.' is required.
Parameters
- 
     float $check
- The value the test for decimal.
- 
     integer $placesoptional null
- Decimal places.
- 
     string $regexoptional null
- If a custom regular expression is used, this is the only validation that will occur.
Returns
booleanSuccess
email()source public static
email( string $check , boolean $deep false , string $regex null )Validates for an email address.
Only uses getmxrr() checking for deep validation if PHP 5.3.0+ is used, or any PHP version on a non-Windows distribution
Parameters
- 
     string $check
- Value to check
- 
     boolean $deepoptional false
- Perform a deeper validation (if true), by also checking availability of host
- 
     string $regexoptional null
- Regex to use (if none it will use built in regex)
Returns
booleanSuccess
equalTo()source public static
equalTo( mixed $check , mixed $comparedTo )Check that value is exactly $comparedTo.
Parameters
- 
     mixed $check
- Value to check
- 
     mixed $comparedTo
- Value to compare
Returns
booleanSuccess
extension()source public static
extension( string|array $check , array $extensions array('gif', 'jpeg', 'png', 'jpg') )Check that value has a valid file extension.
Parameters
- 
     string|array $check
- Value to check
- 
     array $extensionsoptional array('gif', 'jpeg', 'png', 'jpg')
- file extensions to allow. By default extensions are 'gif', 'jpeg', 'png', 'jpg'
Returns
booleanSuccess
fileSize()source public static
fileSize( string|array $check , string $operator null , integer|string $size null )Checks the filesize
Parameters
- 
     string|array $check
- Value to check.
- 
     string $operatoroptional null
- 
     See Validation::comparison().
- 
     integer|string $sizeoptional null
- Size in bytes or human readable string like '5MB'.
Returns
booleanSuccess
inList()source public static
inList( string $check , array $list , boolean $caseInsensitive false )Checks if a value is in a given list. Comparison is case sensitive by default.
Parameters
- 
     string $check
- Value to check.
- 
     array $list
- List to check against.
- 
     boolean $caseInsensitiveoptional false
- Set to true for case insensitive comparison.
Returns
booleanSuccess.
ip()source public static
ip( string $check , string $type 'both' )Validation of an IP address.
Parameters
- 
     string $check
- The string to test.
- 
     string $typeoptional 'both'
- The IP Protocol version to validate against
Returns
booleanSuccess
lengthBetween()source public static
lengthBetween( string $check , integer $min , integer $max )Checks that a string length is within s specified range. Spaces are included in the character count. Returns true is string matches value min, max, or between min and max,
Parameters
- 
     string $check
- Value to check for length
- 
     integer $min
- Minimum value in range (inclusive)
- 
     integer $max
- Maximum value in range (inclusive)
Returns
booleanSuccess
luhn()source public static
luhn( string|array $check , boolean $deep false )Luhn algorithm
Parameters
- 
     string|array $check
- Value to check.
- 
     boolean $deepoptional false
- If true performs deep check.
Returns
booleanSuccess
See
http://en.wikipedia.org/wiki/Luhn_algorithmmaxLength()source public static
maxLength( string $check , integer $max )Checks whether the length of a string (in characters) is smaller or equal to a maximal length..
Parameters
- 
     string $check
- The string to test
- 
     integer $max
- The maximal string length
Returns
booleanSuccess
maxLengthBytes()source public static
maxLengthBytes( string $check , integer $max )Checks whether the length of a string (in bytes) is smaller or equal to a maximal length..
Parameters
- 
     string $check
- The string to test
- 
     integer $max
- The maximal string length
Returns
booleanSuccess
mimeType()source public static
mimeType( string|array $check , array|string $mimeTypes array() )Checks the mime type of a file.
Parameters
- 
     string|array $check
- Value to check.
- 
     array|string $mimeTypesoptional array()
- Array of mime types or regex pattern to check.
Returns
booleanSuccess
Throws
CakeExceptionwhen mime type can not be determined.
minLength()source public static
minLength( string $check , integer $min )Checks whether the length of a string (in characters) is greater or equal to a minimal length.
Parameters
- 
     string $check
- The string to test
- 
     integer $min
- The minimal string length
Returns
booleanSuccess
minLengthBytes()source public static
minLengthBytes( string $check , integer $min )Checks whether the length of a string (in bytes) is greater or equal to a minimal length.
Parameters
- 
     string $check
- The string to test
- 
     integer $min
- The minimal string length
Returns
booleanSuccess
money()source public static
money( string $check , string $symbolPosition 'left' )Checks that a value is a monetary amount.
Parameters
- 
     string $check
- Value to check
- 
     string $symbolPositionoptional 'left'
- Where symbol is located (left/right)
Returns
booleanSuccess
multiple()source public static
multiple( array $check , array $options array() , boolean $caseInsensitive false )Validate a multiple select. Comparison is case sensitive by default.
Valid Options
- in => provide a list of choices that selections must be made from
- max => maximum number of non-zero choices that can be made
- min => minimum number of non-zero choices that can be made
Parameters
- 
     array $check
- Value to check
- 
     array $optionsoptional array()
- Options for the check.
- 
     boolean $caseInsensitiveoptional false
- Set to true for case insensitive comparison.
Returns
booleanSuccess
naturalNumber()source public static
naturalNumber( string $check , boolean $allowZero false )Checks if a value is a natural number.
Parameters
- 
     string $check
- Value to check
- 
     boolean $allowZerooptional false
- Set true to allow zero, defaults to false
Returns
booleanSuccess
See
http://en.wikipedia.org/wiki/Natural_numbernotBlank()source public static
notBlank( string $check )Checks that a string contains something other than whitespace
Returns true if string contains something other than whitespace
Parameters
- 
     string $check
- Value to check
Returns
booleanSuccess
notEmpty()source public static
notEmpty( string|array $check )Backwards compatibility wrapper for Validation::notBlank().
Deprecated
2.7.0 Use Validation::notBlank() instead.Parameters
- 
     string|array $check
- Value to check.
Returns
booleanSuccess.
See
Validation::notBlank()numeric()source public static
numeric( string $check )Checks if a value is numeric.
Parameters
- 
     string $check
- Value to check
Returns
booleanSuccess
phone()source public static
phone( string|array $check , string $regex null , string $country 'all' )Check that a value is a valid phone number.
Parameters
- 
     string|array $check
- Value to check (string or array)
- 
     string $regexoptional null
- Regular expression to use
- 
     string $countryoptional 'all'
- Country code (defaults to 'all')
Returns
booleanSuccess
postal()source public static
postal( string|array $check , string $regex null , string $country 'us' )Checks that a given value is a valid postal code.
Parameters
- 
     string|array $check
- Value to check
- 
     string $regexoptional null
- Regular expression to use
- 
     string $countryoptional 'us'
- Country to use for formatting
Returns
booleanSuccess
range()source public static
range( string $check , integer|float $lower null , integer|float $upper null )Validate that a number is in specified range. if $lower and $upper are not set, will return true if $check is a legal finite on this platform
Parameters
- 
     string $check
- Value to check
- 
     integer|float $loweroptional null
- Lower limit
- 
     integer|float $upperoptional null
- Upper limit
Returns
booleanSuccess
ssn()source public static
ssn( string|array $check , string $regex null , string $country null )Checks that a value is a valid Social Security Number.
Deprecated
Deprecated 2.6. Will be removed in 3.0.Parameters
- 
     string|array $check
- Value to check
- 
     string $regexoptional null
- Regular expression to use
- 
     string $countryoptional null
- Country
Returns
booleanSuccess
time()source public static
time( string $check )Time validation, determines if the string passed is a valid time. Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m) Does not allow/validate seconds.
Parameters
- 
     string $check
- a valid time string
Returns
booleanSuccess
uploadError()source public static
uploadError( string|array $check , boolean $allowNoFile false )Checking for upload errors
Parameters
- 
     string|array $check
- Value to check.
- 
     boolean $allowNoFileoptional false
- Set to true to allow UPLOAD_ERR_NO_FILE as a pass.
Returns
booleanSee
http://www.php.net/manual/en/features.file-upload.errors.phpuploadedFile()source public static
uploadedFile( array $file , array $options array() )Validate an uploaded file.
Helps join uploadError, fileSize and mimeType into one higher level validation method.
Options
- types- A list of valid mime types. If empty all types will be accepted. The- typewill not be looked at, instead the file type will be checked with ext/finfo.
- minSize- The minimum file size. Defaults to not checking.
- maxSize- The maximum file size. Defaults to not checking.
- optional- Whether or not this file is optional. Defaults to false. If true a missing file will pass the validator regardless of other constraints.
Parameters
- 
     array $file
- The uploaded file data from PHP.
- 
     array $optionsoptional array()
- An array of options for the validation.
Returns
booleanurl()source public static
url( string $check , boolean $strict false )Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt
The regex checks for the following component parts:
- a valid, optional, scheme
- a valid ip address OR a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt with an optional port number
- an optional valid path
- an optional query string (get parameters)
- an optional fragment (anchor tag)
Parameters
- 
     string $check
- Value to check
- 
     boolean $strictoptional false
- Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
Returns
booleanSuccess
userDefined()source public static
userDefined( string|array $check , object $object , string $method , array $args null )Runs an user-defined validation.
Parameters
- 
     string|array $check
- value that will be validated in user-defined methods.
- 
     object $object
- class that holds validation method
- 
     string $method
- class method name for validation to run
- 
     array $argsoptional null
- arguments to send to method
Returns
mixeduser-defined class class method returns
uuid()source public static
uuid( string $check )Checks that a value is a valid UUID - http://tools.ietf.org/html/rfc4122
Parameters
- 
     string $check
- Value to check
Returns
booleanSuccess
Properties detail
$_patternsource
protected static array
Some complex patterns needed in multiple places
array(
    'hostname' => '(?:[_\p{L}0-9][-_\p{L}0-9]*\.)*(?:[\p{L}0-9][-\p{L}0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,})'
)$errorssource
public static array
Holds an array of errors messages set in this class. These are used for debugging purposes
array()© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
 https://api.cakephp.org/2.10/class-Validation.html