Class Validation
Validation Class. Used for validation of model data
Offers different validation methods.
Method Detail
_checksource 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
boolean
Success of match
_getDateStringsource protected static
_getDateString( array $value )
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.
Parameters
- array
$value
- The array representing a date or datetime.
Returns
string
string
_populateIpsource protected static
_populateIp( )
Lazily populate the IP address patterns used for validations
alphaNumericsource 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: ['check' => 'valueToCheck'];
Parameters
- string|array
$check
- Value to check
Returns
boolean
Success
asciisource public static
ascii( string $value )
Check that the input value is within the ascii byte range.
This method will reject all non-string values.
Parameters
- string
$value
- The value to check
Returns
boolean
bool
blanksource 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: ['check' => 'valueToCheck'];
Deprecated
3.0.2Parameters
- string|array
$check
- Value to check
Returns
boolean
Success
booleansource 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
boolean
Success
ccsource public static
cc( string|array $check , string|array $type 'fast' , boolean $deep false , string|null $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
$type
optional 'fast' - 'all' may be passed as a string, 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: ['amex', 'bankcard', 'maestro']
- boolean
$deep
optional false - set to true this will check the Luhn algorithm of the credit card.
- string|null
$regex
optional null - A custom regex can also be passed, this will be used instead of the defined regex values
Returns
boolean
Success
See
Cake\Validation\Validation::luhn()
compareWithsource public static
compareWith( mixed $check , string $field , array $context )
Compare one field to another.
If both fields have exactly the same value this method will return true.
Parameters
- mixed
$check
- The value to find in $field.
- string
$field
- The field to check $check against. This field must be present in $context.
- array
$context
- The validation context.
Returns
boolean
bool
comparisonsource public static
comparison( string $check1 , string $operator , integer $check2 )
Used to compare 2 numeric values.
Parameters
- string
$check1
- if string is passed for, a string must also be passed for $check2 used as an array it must be passed as ['check1' => value, 'operator' => 'value', 'check2' => value]
- string
$operator
- Can be either a word or operand is greater >, is less <, greater or equal >= less or equal <=, is less <, equal to ==, not equal !=
- integer
$check2
- only needed if $check1 is a string
Returns
boolean
Success
containsNonAlphaNumericsource public static
containsNonAlphaNumeric( string $check , integer $count 1 )
Checks if a string contains one or more non-alphanumeric characters.
Returns true if string contains at least the specified number of non-alphanumeric characters
Parameters
- string
$check
- Value to check
- integer
$count
optional 1 - Number of non-alphanumerics to check for
Returns
boolean
Success
customsource public static
custom( string|array $check , string|null $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: ['check' => value, 'regex' => 'valid regular expression']
- string|null
$regex
optional null - If $check is passed as a string, $regex must also be set to valid regular expression
Returns
boolean
Success
datesource public static
date( string|DateTime $check , string|array $format 'ymd' , string|null $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:
-
dmy
27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash -
mdy
12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash -
ymd
2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash -
dMy
27 December 2006 or 27 Dec 2006 -
Mdy
December 27, 2006 or Dec 27, 2006 comma is optional -
My
December 2006 or Dec 2006 -
my
12/2006 or 12/06 separators can be a space, period, dash, forward slash -
ym
2006/12 or 06/12 separators can be a space, period, dash, forward slash -
y
2006 just the year without any separators
Parameters
- string|DateTime
$check
- a valid date string/object
- string|array
$format
optional 'ymd' - Use a string or an array of the keys above. Arrays should be passed as ['dmy', 'mdy', etc]
- string|null
$regex
optional null - If a custom regular expression is used this is the only validation that will occur.
Returns
boolean
Success
datetimesource public static
datetime( string|DateTime $check , string|array $dateFormat 'ymd' , string|null $regex null )
Validates a datetime value
All values matching the "date" core validation rule, and the "time" one will be valid
Parameters
- string|DateTime
$check
- Value to check
- string|array
$dateFormat
optional 'ymd' - Format of the date part. See Validation::date for more information.
- string|null
$regex
optional null - Regex for the date part. If a custom regular expression is used this is the only validation that will occur.
Returns
boolean
True if the value is valid, false otherwise
See
Cake\Validation\Validation::date()
Cake\Validation\Validation::time()
decimalsource public static
decimal( float $check , integer $places null , string|null $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
$places
optional null - Decimal places.
- string|null
$regex
optional null - If a custom regular expression is used, this is the only validation that will occur.
Returns
boolean
Success
emailsource public static
email( string $check , boolean $deep false , string $regex null )
Validates for an email address.
Only uses getmxrr() checking for deep validation, or any PHP version on a non-windows distribution
Parameters
- string
$check
- Value to check
- boolean
$deep
optional false - Perform a deeper validation (if true), by also checking availability of host
- string
$regex
optional null - Regex to use (if none it will use built in regex)
Returns
boolean
Success
equalTosource public static
equalTo( mixed $check , mixed $comparedTo )
Checks that value is exactly $comparedTo.
Parameters
- mixed
$check
- Value to check
- mixed
$comparedTo
- Value to compare
Returns
boolean
Success
extensionsource public static
extension( string|array $check , array $extensions ['gif', 'jpeg', 'png', 'jpg'] )
Checks that value has a valid file extension.
Parameters
- string|array
$check
- Value to check
- array
$extensions
optional ['gif', 'jpeg', 'png', 'jpg'] - file extensions to allow. By default extensions are 'gif', 'jpeg', 'png', 'jpg'
Returns
boolean
Success
fileSizesource public static
fileSize( string|array $check , string|null $operator null , integer|string|null $size null )
Checks the filesize
Parameters
- string|array
$check
- Value to check.
- string|null
$operator
optional null - See
Validation::comparison()
. - integer|string|null
$size
optional null - Size in bytes or human readable string like '5MB'.
Returns
boolean
Success
geoCoordinatesource public static
geoCoordinate( string $value , array $options [] )
Validates a geographic coordinate.
Supported formats:
-
<latitude>, <longitude>
Example:-25.274398, 133.775136
Options
-
type
- A string of the coordinate format, right now onlylatLong
. -
format
- By defaultboth
, can belong
andlat
as well to validate only a part of the coordinate.
Parameters
- string
$value
- Geographic location as string
- array
$options
optional [] - Options for the validation logic.
Returns
boolean
bool
inListsource 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
$caseInsensitive
optional false - Set to true for case insensitive comparison.
Returns
boolean
Success.
ipsource public static
ip( string $check , string $type 'both' )
Validation of an IP address.
Parameters
- string
$check
- The string to test.
- string
$type
optional 'both' - The IP Protocol version to validate against
Returns
boolean
Success
isIntegersource public static
isInteger( string $value )
Check that the input value is an integer
This method will accept strings that contain only integer data as well.
Parameters
- string
$value
- The value to check
Returns
boolean
bool
latitudesource public static
latitude( string $value , array $options [] )
Convenience method for latitude validation.
Parameters
- string
$value
- Latitude as string
- array
$options
optional [] - Options for the validation logic.
Returns
boolean
bool
See
Cake\Validation\Validation::geoCoordinate()
Link
https://en.wikipedia.org/wiki/LatitudelengthBetweensource public static
lengthBetween( string $check , integer $min , integer $max )
Checks that a string length is within specified range. Spaces are included in the character count. Returns true if 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
boolean
Success
longitudesource public static
longitude( string $value , array $options [] )
Convenience method for longitude validation.
Parameters
- string
$value
- Latitude as string
- array
$options
optional [] - Options for the validation logic.
Returns
boolean
bool
See
Cake\Validation\Validation::geoCoordinate()
Link
https://en.wikipedia.org/wiki/Longitudeluhnsource public static
luhn( string|array $check )
Luhn algorithm
Parameters
- string|array
$check
- Value to check.
Returns
boolean
Success
See
http://en.wikipedia.org/wiki/Luhn_algorithmmaxLengthsource public static
maxLength( string $check , integer $max )
Checks whether the length of a string is smaller or equal to a maximal length..
Parameters
- string
$check
- The string to test
- integer
$max
- The maximal string length
Returns
boolean
Success
mimeTypesource public static
mimeType( string|array $check , array|string $mimeTypes [] )
Checks the mime type of a file.
Parameters
- string|array
$check
- Value to check.
- array|string
$mimeTypes
optional [] - Array of mime types or regex pattern to check.
Returns
boolean
Success
Throws
RuntimeException
when mime type can not be determined.
LogicException
when ext/fileinfo is missing
minLengthsource public static
minLength( string $check , integer $min )
Checks whether the length of a string is greater or equal to a minimal length.
Parameters
- string
$check
- The string to test
- integer
$min
- The minimal string length
Returns
boolean
Success
moneysource public static
money( string $check , string $symbolPosition 'left' )
Checks that a value is a monetary amount.
Parameters
- string
$check
- Value to check
- string
$symbolPosition
optional 'left' - Where symbol is located (left/right)
Returns
boolean
Success
multiplesource public static
multiple( array $check , array $options [] , boolean $caseInsensitive false )
Validates 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
$options
optional [] - Options for the check.
- boolean
$caseInsensitive
optional false - Set to true for case insensitive comparison.
Returns
boolean
Success
naturalNumbersource public static
naturalNumber( string $check , boolean $allowZero false )
Checks if a value is a natural number.
Parameters
- string
$check
- Value to check
- boolean
$allowZero
optional false - Set true to allow zero, defaults to false
Returns
boolean
Success
See
http://en.wikipedia.org/wiki/Natural_numbernotBlanksource public static
notBlank( string|array $check )
Checks that a string contains something other than whitespace
Returns true if string contains something other than whitespace
$check can be passed as an array: ['check' => 'valueToCheck'];
Parameters
- string|array
$check
- Value to check
Returns
boolean
Success
notEmptysource public static
notEmpty( string|array $check )
Backwards compatibility wrapper for Validation::notBlank().
Deprecated
3.0.2 Use Validation::notBlank() instead.Parameters
- string|array
$check
- Value to check.
Returns
boolean
Success.
See
Cake\Validation\Validation::notBlank()
numericsource public static
numeric( string $check )
Checks if a value is numeric.
Parameters
- string
$check
- Value to check
Returns
boolean
Success
rangesource public static
range( string $check , integer|float|null $lower null , integer|float|null $upper null )
Validates that a number is in specified range.
If $lower and $upper are set, the range is inclusive. If they are not set, will return true if $check is a legal finite on this platform.
Parameters
- string
$check
- Value to check
- integer|float|null
$lower
optional null - Lower limit
- integer|float|null
$upper
optional null - Upper limit
Returns
boolean
Success
timesource public static
time( string|DateTime $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|DateTime
$check
- a valid time string/object
Returns
boolean
Success
uploadErrorsource public static
uploadError( string|array $check , boolean $allowNoFile false )
Checking for upload errors
Parameters
- string|array
$check
- Value to check.
- boolean
$allowNoFile
optional false - Set to true to allow UPLOAD_ERR_NO_FILE as a pass.
Returns
boolean
bool
See
http://www.php.net/manual/en/features.file-upload.errors.phpuploadedFilesource public static
uploadedFile( array $file , array $options [] )
Validate an uploaded file.
Helps join uploadError
, fileSize
and mimeType
into one higher level validation method.
Options
-
types
- An array of valid mime types. If empty all types will be accepted. Thetype
will not be looked at, instead the file type will be checked with ext/finfo. -
minSize
- The minimum file size in bytes. Defaults to not checking. -
maxSize
- The maximum file size in bytes. 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
$options
optional [] - An array of options for the validation.
Returns
boolean
bool
urlsource 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
$strict
optional false - Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
Returns
boolean
Success
userDefinedsource public static
userDefined( string|array $check , object $object , string $method , array|null $args null )
Runs an user-defined validation.
Deprecated
3.0.2 You can just set a callable forrule
key when adding validators.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|null
$args
optional null - arguments to send to method
Returns
mixed
user-defined class class method returns
utf8source public static
utf8( string $value , array $options [] )
Check that the input value is a utf8 string.
This method will reject all non-string values.
# Options
-
extended
- Disallow bytes higher within the basic multilingual plane. MySQL's older utf8 encoding type does not allow characters above the basic multilingual plane. Defaults to false.
Parameters
- string
$value
- The value to check
- array
$options
optional [] - An array of options. See above for the supported options.
Returns
boolean
bool
uuidsource 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
boolean
Success
Constants summary
string | DEFAULT_LOCALE Default locale | 'en_US' |
Properties summary
© 2005–2016 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.
http://api.cakephp.org/3.1/class-Cake.Validation.Validation.html