Validation Class. Used for validation of model data
Offers different validation methods.
string
'=='
Equal to comparison operator.
string
'>'
Greater than comparison operator.
string
'>='
Greater than or equal to comparison operator.
string
'<'
Less than comparison operator.
string
'<='
Less than or equal to comparison operator.
string
'!='
Not equal to comparison operator.
string
'!=='
Not same as comparison operator.
string
'==='
Same as operator.
array<string>
[self::COMPARE_EQUAL, self::COMPARE_NOT_EQUAL, self::COMPARE_SAME, self::COMPARE_NOT_SAME]
string
'iso8601'
Datetime ISO8601 format
string
'en_US'
Default locale
Runs a regular expression match.
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.
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.
Check that the input value is within the ascii byte range.
Checks that a string contains only ascii integer or letters.
Validates if passed value is boolean-like.
Compare one field to another.
Compare one field to another.
Used to compare 2 numeric values.
Checks if a string contains one or more non-alphanumeric characters.
Validation of credit card numbers. Returns true if $check is in the proper credit card format.
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.
Checks that value is exactly $comparedTo.
Checks that value has a valid file extension.
Validates if given value is falsey.
Checks the filesize
Validates a geographic coordinate.
Helper for reading the file out of the various file implementations we accept.
Check that the input value is a 6 digits hex color.
Check that the input value has a valid International Bank Account Number IBAN syntax Requirements are uppercase, no whitespaces, max length 34, country code and checksum exist at right spots, body matches against checksum via Mod97-10 algorithm
Validates the image height.
Validates the size of an uploaded image.
Validates the image width.
Checks if a value is in a given list. Comparison is case sensitive by default.
Validation of an IP address.
Check that the input value is an array.
Check that the input value is an integer
Check that the input value is a scalar.
Validates an iso8601 datetime format ISO8601 recognize datetime like 2019 as a valid date. To validate and check date integrity, use @see \Cake\Validation\Validation::datetime()
Convenience method for latitude validation.
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.
Convenience method for longitude validation.
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.
Validates a multiple select. Comparison is case sensitive by default.
Checks if a value is a natural number.
Checks that a doesn't contain any alpha numeric characters
Checks that a doesn't contain any non-ascii alpha numeric characters
Checks that a string contains something other than whitespace
Used to check the count of a given value of type array or Countable.
Checks if a value is numeric.
Validates that a number is in specified range.
Time validation, determines if the string passed is a valid time. Validates time as 24hr (HH:MM[:SS][.FFFFFF]) or am/pm ([H]H:MM[a|p]m)
Validates if given value is truthy.
Checking for upload errors
Validate an uploaded file.
Checks that a value is a valid URL according to https://www.w3.org/Addressing/URL/url-spec.txt
Check that the input value is a utf8 string.
Checks that a value is a valid UUID - https://tools.ietf.org/html/rfc4122
_check(mixed $check, string $regex): bool
Runs a regular expression match.
mixed
$check Value to check against the $regex expression
string
$regex Regular expression
bool
_getDateString(array<string, mixed> $value): string
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.
array<string, mixed>
$value The array representing a date or datetime.
string
_populateIp(): void
Lazily populate the IP address patterns used for validations
void
_reset(): void
Reset internal variables for another validation run.
void
alphaNumeric(mixed $check): bool
Checks that a string contains only integer or letters.
This method's definition of letters and integers includes unicode characters. Use asciiAlphaNumeric()
if you want to exclude unicode.
mixed
$check Value to check
bool
ascii(mixed $value): bool
Check that the input value is within the ascii byte range.
This method will reject all non-string values.
mixed
$value The value to check
bool
asciiAlphaNumeric(mixed $check): bool
Checks that a string contains only ascii integer or letters.
mixed
$check Value to check
bool
boolean(string|int|bool $check, array<string|int|bool> $booleanValues = []): bool
Validates if passed value is boolean-like.
The list of what is considered to be boolean values, may be set via $booleanValues.
string|int|bool
$check Value to check.
array<string|int|bool>
$booleanValues optional List of valid boolean values, defaults to [true, false, 0, 1, '0', '1']
.
bool
compareFields(mixed $check, string $field, string $operator, array<string, mixed> $context): bool
Compare one field to another.
Return true if the comparison matches the expected result.
mixed
$check The value to find in $field.
string
$field The field to check $check against. This field must be present in $context.
string
$operator Comparison operator. See Validation::comparison().
array<string, mixed>
$context The validation context.
bool
compareWith(mixed $check, string $field, array<string, mixed> $context): bool
Compare one field to another.
If both fields have exactly the same value this method will return true.
mixed
$check The value to find in $field.
string
$field The field to check $check against. This field must be present in $context.
array<string, mixed>
$context The validation context.
bool
comparison(string|int $check1, string $operator, string|int $check2): bool
Used to compare 2 numeric values.
string|int
$check1 The left value to compare.
string
$operator Can be one of following operator strings: '>', '<', '>=', '<=', '==', '!=', '===' and '!=='. You can use one of the Validation::COMPARE_* constants.
string|int
$check2 The right value to compare.
bool
containsNonAlphaNumeric(mixed $check, int $count = 1): bool
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
mixed
$check Value to check
int
$count optional Number of non-alphanumerics to check for
bool
creditCard(mixed $check, array<string>|string $type = 'fast', bool $deep = false, string|null $regex = null): bool
Validation of credit card numbers. Returns true if $check is in the proper credit card format.
mixed
$check credit card number to validate
array<string>|string
$type optional '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']
bool
$deep optional set to true this will check the Luhn algorithm of the credit card.
string|null
$regex optional A custom regex, this will be used instead of the defined regex values.
bool
custom(mixed $check, string|null $regex = null): bool
Used when a custom regular expression is needed.
mixed
$check The value to check.
string|null
$regex optional If $check is passed as a string, $regex must also be set to valid regular expression
bool
date(mixed $check, array<string>|string $format = 'ymd', string|null $regex = null): bool
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 0001 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 separatorsmixed
$check a valid date string/object
array<string>|string
$format optional Use a string or an array of the keys above. Arrays should be passed as ['dmy', 'mdy', ...]
string|null
$regex optional If a custom regular expression is used this is the only validation that will occur.
bool
datetime(mixed $check, array|string $dateFormat = 'ymd', string|null $regex = null): bool
Validates a datetime value
All values matching the "date" core validation rule, and the "time" one will be valid
mixed
$check Value to check
array|string
$dateFormat optional Format of the date part. See Validation::date() for more information. Or Validation::DATETIME_ISO8601
to validate an ISO8601 datetime value.
string|null
$regex optional Regex for the date part. If a custom regular expression is used this is the only validation that will occur.
bool
decimal(mixed $check, int|true|null $places = null, string|null $regex = null): bool
Checks that a value is a valid decimal. Both the sign and exponent are optional.
Valid Places:
mixed
$check The value the test for decimal.
int|true|null
$places optional Decimal places.
string|null
$regex optional If a custom regular expression is used, this is the only validation that will occur.
bool
email(mixed $check, bool $deep = false, string|null $regex = null): bool
Validates for an email address.
Only uses getmxrr() checking for deep validation, or any PHP version on a non-windows distribution
mixed
$check Value to check
bool
$deep optional Perform a deeper validation (if true), by also checking availability of host
string|null
$regex optional Regex to use (if none it will use built in regex)
bool
equalTo(mixed $check, mixed $comparedTo): bool
Checks that value is exactly $comparedTo.
mixed
$check Value to check
mixed
$comparedTo Value to compare
bool
extension(Psr\Http\Message\UploadedFileInterface|array|string $check, array<string> $extensions = ['gif', 'jpeg', 'png', 'jpg']): bool
Checks that value has a valid file extension.
Psr\Http\Message\UploadedFileInterface|array|string
$check Value to check
array<string>
$extensions optional file extensions to allow. By default extensions are 'gif', 'jpeg', 'png', 'jpg'
bool
falsey(string|int|bool $check, array<string|int|bool> $falseyValues = []): bool
Validates if given value is falsey.
The list of what is considered to be falsey values, may be set via $falseyValues.
string|int|bool
$check Value to check.
array<string|int|bool>
$falseyValues optional List of valid falsey values, defaults to [false, 0, '0']
.
bool
fileSize(Psr\Http\Message\UploadedFileInterface|array|string $check, string $operator, string|int $size): bool
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.
Psr\Http\Message\UploadedFileInterface|array|string
$check Value to check.
string
$operator See Validation::comparison()
.
string|int
$size Size in bytes or human readable string like '5MB'.
bool
geoCoordinate(mixed $value, array<string, mixed> $options = []): bool
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.mixed
$value Geographic location as string
array<string, mixed>
$options optional Options for the validation logic.
bool
getFilename(mixed $check): string|false
Helper for reading the file out of the various file implementations we accept.
mixed
$check The data to read a filename out of.
string|false
hexColor(mixed $check): bool
Check that the input value is a 6 digits hex color.
mixed
$check The value to check
bool
iban(mixed $check): bool
Check that the input value has a valid International Bank Account Number IBAN syntax Requirements are uppercase, no whitespaces, max length 34, country code and checksum exist at right spots, body matches against checksum via Mod97-10 algorithm
mixed
$check The value to check
bool
imageHeight(mixed $file, string $operator, int $height): bool
Validates the image height.
mixed
$file The uploaded file data from PHP.
string
$operator Comparison operator.
int
$height Min or max height.
bool
imageSize(mixed $file, array<string, mixed> $options): bool
Validates the size of an uploaded image.
mixed
$file The uploaded file data from PHP.
array<string, mixed>
$options Options to validate width and height.
bool
InvalidArgumentException
imageWidth(mixed $file, string $operator, int $width): bool
Validates the image width.
mixed
$file The uploaded file data from PHP.
string
$operator Comparison operator.
int
$width Min or max width.
bool
inList(mixed $check, array<string> $list, bool $caseInsensitive = false): bool
Checks if a value is in a given list. Comparison is case sensitive by default.
mixed
$check Value to check.
array<string>
$list List to check against.
bool
$caseInsensitive optional Set to true for case insensitive comparison.
bool
ip(mixed $check, string $type = 'both'): bool
Validation of an IP address.
mixed
$check The string to test.
string
$type optional The IP Protocol version to validate against
bool
isArray(mixed $value): bool
Check that the input value is an array.
mixed
$value The value to check
bool
isInteger(mixed $value): bool
Check that the input value is an integer
This method will accept strings that contain only integer data as well.
mixed
$value The value to check
bool
isScalar(mixed $value): bool
Check that the input value is a scalar.
This method will accept integers, floats, strings and booleans, but not accept arrays, objects, resources and nulls.
mixed
$value The value to check
bool
iso8601(mixed $check): bool
Validates an iso8601 datetime format ISO8601 recognize datetime like 2019 as a valid date. To validate and check date integrity, use @see \Cake\Validation\Validation::datetime()
mixed
$check Value to check
bool
latitude(mixed $value, array<string, mixed> $options = []): bool
Convenience method for latitude validation.
mixed
$value Latitude as string
array<string, mixed>
$options optional Options for the validation logic.
bool
lengthBetween(mixed $check, int $min, int $max): bool
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,
mixed
$check Value to check for length
int
$min Minimum value in range (inclusive)
int
$max Maximum value in range (inclusive)
bool
localizedTime(mixed $check, string $type = 'datetime', string|int|null $format = null): bool
Date and/or time string validation. Uses I18n::Time
to parse the date. This means parsing is locale dependent.
mixed
$check a date string or object (will always pass)
string
$type optional Parser type, one out of 'date', 'time', and 'datetime'
string|int|null
$format optional any format accepted by IntlDateFormatter
bool
InvalidArgumentException
longitude(mixed $value, array<string, mixed> $options = []): bool
Convenience method for longitude validation.
mixed
$value Latitude as string
array<string, mixed>
$options optional Options for the validation logic.
bool
luhn(mixed $check): bool
Luhn algorithm
mixed
$check Value to check.
bool
maxLength(mixed $check, int $max): bool
Checks whether the length of a string (in characters) is smaller or equal to a maximal length.
mixed
$check The string to test
int
$max The maximal string length
bool
maxLengthBytes(mixed $check, int $max): bool
Checks whether the length of a string (in bytes) is smaller or equal to a maximal length.
mixed
$check The string to test
int
$max The maximal string length
bool
mimeType(Psr\Http\Message\UploadedFileInterface|array|string $check, array|string $mimeTypes = []): bool
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.
Psr\Http\Message\UploadedFileInterface|array|string
$check Value to check.
array|string
$mimeTypes optional Array of mime types or regex pattern to check.
bool
RuntimeException
LogicException
minLength(mixed $check, int $min): bool
Checks whether the length of a string (in characters) is greater or equal to a minimal length.
mixed
$check The string to test
int
$min The minimal string length
bool
minLengthBytes(mixed $check, int $min): bool
Checks whether the length of a string (in bytes) is greater or equal to a minimal length.
mixed
$check The string to test
int
$min The minimal string length (in bytes)
bool
money(mixed $check, string $symbolPosition = 'left'): bool
Checks that a value is a monetary amount.
mixed
$check Value to check
string
$symbolPosition optional Where symbol is located (left/right)
bool
multiple(mixed $check, array<string, mixed> $options = [], bool $caseInsensitive = false): bool
Validates a multiple select. Comparison is case sensitive by default.
Valid Options
mixed
$check Value to check
array<string, mixed>
$options optional Options for the check.
bool
$caseInsensitive optional Set to true for case insensitive comparison.
bool
naturalNumber(mixed $check, bool $allowZero = false): bool
Checks if a value is a natural number.
mixed
$check Value to check
bool
$allowZero optional Set true to allow zero, defaults to false
bool
notAlphaNumeric(mixed $check): bool
Checks that a doesn't contain any alpha numeric characters
This method's definition of letters and integers includes unicode characters. Use notAsciiAlphaNumeric()
if you want to exclude ascii only.
mixed
$check Value to check
bool
notAsciiAlphaNumeric(mixed $check): bool
Checks that a doesn't contain any non-ascii alpha numeric characters
mixed
$check Value to check
bool
notBlank(mixed $check): bool
Checks that a string contains something other than whitespace
Returns true if string contains something other than whitespace
mixed
$check Value to check
bool
numElements(mixed $check, string $operator, int $expectedCount): bool
Used to check the count of a given value of type array or Countable.
mixed
$check The value to check the count on.
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 !=
int
$expectedCount The expected count value.
bool
numeric(mixed $check): bool
Checks if a value is numeric.
mixed
$check Value to check
bool
range(mixed $check, float|null $lower = null, float|null $upper = null): bool
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.
mixed
$check Value to check
float|null
$lower optional Lower limit
float|null
$upper optional Upper limit
bool
time(mixed $check): bool
Time validation, determines if the string passed is a valid time. Validates time as 24hr (HH:MM[:SS][.FFFFFF]) or am/pm ([H]H:MM[a|p]m)
Seconds and fractional seconds (microseconds) are allowed but optional in 24hr format.
mixed
$check a valid time string/object
bool
truthy(string|int|bool $check, array<string|int|bool> $truthyValues = []): bool
Validates if given value is truthy.
The list of what is considered to be truthy values, may be set via $truthyValues.
string|int|bool
$check Value to check.
array<string|int|bool>
$truthyValues optional List of valid truthy values, defaults to [true, 1, '1']
.
bool
uploadError(Psr\Http\Message\UploadedFileInterface|array|string $check, bool $allowNoFile = false): bool
Checking for upload errors
Psr\Http\Message\UploadedFileInterface|array|string
$check Value to check.
bool
$allowNoFile optional Set to true to allow UPLOAD_ERR_NO_FILE as a pass.
bool
uploadedFile(mixed $file, array<string, mixed> $options = []): bool
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 this file is optional. Defaults to false. If true a missing file will pass the validator regardless of other constraints.mixed
$file The uploaded file data from PHP.
array<string, mixed>
$options optional An array of options for the validation.
bool
url(mixed $check, bool $strict = false): bool
Checks that a value is a valid URL according to https://www.w3.org/Addressing/URL/url-spec.txt
The regex checks for the following component parts:
mixed
$check Value to check
bool
$strict optional Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
bool
utf8(mixed $value, array<string, mixed> $options = []): bool
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.mixed
$value The value to check
array<string, mixed>
$options optional An array of options. See above for the supported options.
bool
uuid(mixed $check): bool
Checks that a value is a valid UUID - https://tools.ietf.org/html/rfc4122
mixed
$check Value to check
bool
Some complex patterns needed in multiple places
array<string, string>
Holds an array of errors messages set in this class. These are used for debugging purposes
array
© 2005–present 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/4.4/class-Cake.Validation.Validation.html