Validation Class. Used for validation of model data
Offers different validation methods.
string
'en_US'
array
array
Holds an array of errors messages set in this class. These are used for debugging purposes
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.
Returns true if field is left blank -OR- only whitespace characters are present in its value Whitespace characters include Space, Tab, Carriage Return, Newline
Validation of credit card numbers. Returns true if $check is in the proper credit card format.
Date validation, determines if the string passed is a valid date. keys that expect full month, day and year will validate leap years.
Helper for reading the file out of the various file implementations we accept.
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,
Date and/or time string validation. Uses I18n::Time
to parse the date. This means parsing is locale dependent.
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.
_check( string $check , string $regex )
Runs a regular expression match.
$check
$regex
_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.
$value
_populateIp( )
Lazily populate the IP address patterns used for validations
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'];
$check
ascii( string $value )
Check that the input value is within the ascii byte range.
This method will reject all non-string values.
$value
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
boolean( boolean|integer|string $check , array $booleanValues [] )
Validates if passed value is boolean-like.
The list of what is considered to be boolean values, may be set via $booleanValues.
$check
$booleanValues
optional [] [true, false, 0, 1, '0', '1']
.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.
$check
$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']
$deep
optional false $regex
optional null 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.
$check
$field
$context
comparison( string $check1 , string $operator , integer $check2 )
Used to compare 2 numeric values.
$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]
$operator
Can be either a word or operand is greater >, is less <, greater or equal >= less or equal <=, is less <, equal to ==, not equal !=
$check2
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
$check
$count
optional 1 custom( string|array $check , string|null $regex null )
Used when a custom regular expression is needed.
$check
When used as a string, $regex must also be a valid regular expression. As and array: ['check' => value, 'regex' => 'valid regular expression']
$regex
optional null date( string|DateTimeInterface $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.
dmy
27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slashmdy
12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slashymd
2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slashdMy
27 December 2006 or 27 Dec 2006Mdy
December 27, 2006 or Dec 27, 2006 comma is optionalMy
December 2006 or Dec 2006my
12/2006 or 12/06 separators can be a space, period, dash, forward slashym
2006/12 or 06/12 separators can be a space, period, dash, forward slashy
2006 just the year without any separators$check
$format
optional 'ymd' Use a string or an array of the keys above. Arrays should be passed as ['dmy', 'mdy', etc]
$regex
optional null datetime( string|DateTimeInterface $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
$check
$dateFormat
optional 'ymd' $regex
optional null decimal( float $check , integer|null $places null , string|null $regex null )
Checks that a value is a valid decimal. Both the sign and exponent are optional.
Valid Places:
$check
$places
optional null $regex
optional null email( string $check , boolean $deep false , string|null $regex null )
Validates for an email address.
Only uses getmxrr() checking for deep validation, or any PHP version on a non-windows distribution
$check
$deep
optional false $regex
optional null equalTo( mixed $check , mixed $comparedTo )
Checks that value is exactly $comparedTo.
$check
$comparedTo
extension( string|array $check , array $extensions ['gif', 'jpeg', 'png', 'jpg'] )
Checks that value has a valid file extension.
$check
$extensions
optional ['gif', 'jpeg', 'png', 'jpg'] falsey( boolean|integer|string $check , array $falseyValues [] )
Validates if given value is falsey.
The list of what is considered to be falsey values, may be set via $falseyValues.
$check
$falseyValues
optional [] [false, 0, '0']
.fileSize( string|array|Psr\Http\Message\UploadedFileInterface $check , string|null $operator null , integer|string|null $size null )
Checks the filesize
Will check the filesize of files/UploadedFileInterface instances by checking the filesize() on disk and not relying on the length reported by the client.
$check
$operator
optional null Validation::comparison()
.$size
optional null geoCoordinate( string $value , array $options [] )
Validates a geographic coordinate.
Supported formats:
<latitude>, <longitude>
Example: -25.274398, 133.775136
type
- A string of the coordinate format, right now only latLong
.format
- By default both
, can be long
and lat
as well to validate only a part of the coordinate.$value
$options
optional [] getFilename( string|array|Psr\Http\Message\UploadedFileInterface $check )
Helper for reading the file out of the various file implementations we accept.
$check
imageHeight( array $file , string $operator , integer $height )
Validates the image width.
$file
$operator
$height
imageSize( array $file , array $options )
Validates the size of an uploaded image.
$file
$options
imageWidth( array $file , string $operator , integer $width )
Validates the image width.
$file
$operator
$width
inList( string $check , array $list , boolean $caseInsensitive false )
Checks if a value is in a given list. Comparison is case sensitive by default.
$check
$list
$caseInsensitive
optional false ip( string $check , string $type 'both' )
Validation of an IP address.
$check
$type
optional 'both' isArray( array $value )
Check that the input value is an array.
$value
isInteger( string $value )
Check that the input value is an integer
This method will accept strings that contain only integer data as well.
$value
latitude( string $value , array $options [] )
Convenience method for latitude validation.
$value
$options
optional [] 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,
$check
$min
$max
localizedTime( string|DateTime $check , string $type 'datetime' , string|integer|null $format null )
Date and/or time string validation. Uses I18n::Time
to parse the date. This means parsing is locale dependent.
$check
$type
optional 'datetime' $format
optional null longitude( string $value , array $options [] )
Convenience method for longitude validation.
$value
$options
optional [] luhn( string|array $check )
Luhn algorithm
$check
maxLength( string $check , integer $max )
Checks whether the length of a string (in characters) is smaller or equal to a maximal length.
$check
$max
maxLengthBytes( string $check , integer $max )
Checks whether the length of a string (in bytes) is smaller or equal to a maximal length.
$check
$max
mimeType( string|array|Psr\Http\Message\UploadedFileInterface $check , array|string $mimeTypes [] )
Checks the mime type of a file.
Will check the mimetype of files/UploadedFileInterface instances by checking the using finfo on the file, not relying on the content-type sent by the client.
$check
$mimeTypes
optional [] minLength( string $check , integer $min )
Checks whether the length of a string (in characters) is greater or equal to a minimal length.
$check
$min
minLengthBytes( string $check , integer $min )
Checks whether the length of a string (in bytes) is greater or equal to a minimal length.
$check
$min
money( string $check , string $symbolPosition 'left' )
Checks that a value is a monetary amount.
$check
$symbolPosition
optional 'left' multiple( array $check , array $options [] , boolean $caseInsensitive false )
Validates a multiple select. Comparison is case sensitive by default.
Valid Options
$check
$options
optional [] $caseInsensitive
optional false naturalNumber( string $check , boolean $allowZero false )
Checks if a value is a natural number.
$check
$allowZero
optional false 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'];
$check
notEmpty( string|array $check )
Backwards compatibility wrapper for Validation::notBlank().
$check
numElements( array|Countable $check , string $operator , integer $expectedCount )
Used to check the count of a given value of type array or Countable.
$check
$operator
Can be either a word or operand is greater >, is less <, greater or equal >= less or equal <=, is less <, equal to ==, not equal !=
$expectedCount
numeric( string $check )
Checks if a value is numeric.
$check
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.
$check
$lower
optional null $upper
optional null time( string|DateTimeInterface $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.
$check
truthy( boolean|integer|string $check , array $truthyValues [] )
Validates if given value is truthy.
The list of what is considered to be truthy values, may be set via $truthyValues.
$check
$truthyValues
optional [] [true, 1, '1']
.uploadError( string|array|Psr\Http\Message\UploadedFileInterface $check , boolean $allowNoFile false )
Checking for upload errors
$check
$allowNoFile
optional false uploadedFile( array $file , array $options [] )
Validate an uploaded file.
Helps join uploadError
, fileSize
and mimeType
into one higher level validation method.
types
- An array of valid mime types. If empty all types will be accepted. The type
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.$file
$options
optional [] 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:
$check
$strict
optional false userDefined( string|array $check , object $object , string $method , array|null $args null )
Runs an user-defined validation.
rule
key when adding validators.$check
$object
$method
$args
optional null utf8( string $value , array $options [] )
Check that the input value is a utf8 string.
This method will reject all non-string values.
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.$value
$options
optional [] uuid( string $check )
Checks that a value is a valid UUID - http://tools.ietf.org/html/rfc4122
$check
protected static array
Some complex patterns needed in multiple places
[ '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,})', 'latitude' => '[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?)', 'longitude' => '[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)', ]
public static array
Holds an array of errors messages set in this class. These are used for debugging purposes
[]
© 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/3.4/class-Cake.Validation.Validation.html