Fr3nch13/CakePHP Utilities

Validation
in package

Validation Class. Used for validation of model data

Offers different validation methods.

Table of Contents

Constants

COMPARE_EQUAL  = '=='
Equal to comparison operator.
COMPARE_GREATER  = '>'
Greater than comparison operator.
COMPARE_GREATER_OR_EQUAL  = '>='
Greater than or equal to comparison operator.
COMPARE_LESS  = '<'
Less than comparison operator.
COMPARE_LESS_OR_EQUAL  = '<='
Less than or equal to comparison operator.
COMPARE_NOT_EQUAL  = '!='
Not equal to comparison operator.
COMPARE_NOT_SAME  = '!=='
Not same as comparison operator.
COMPARE_SAME  = '==='
Same as operator.
DATETIME_ISO8601  = 'iso8601'
Datetime ISO8601 format
DEFAULT_LOCALE  = 'en_US'
Default locale
COMPARE_STRING  = [self::COMPARE_EQUAL, self::COMPARE_NOT_EQUAL, self::COMPARE_SAME, self::COMPARE_NOT_SAME]

Properties

$errors  : array<string|int, mixed>
Holds an array of errors messages set in this class.
$_pattern  : array<string, string>
Some complex patterns needed in multiple places

Methods

alphaNumeric()  : bool
Checks that a string contains only integer or letters.
ascii()  : bool
Check that the input value is within the ascii byte range.
asciiAlphaNumeric()  : bool
Checks that a string contains only ascii integer or letters.
boolean()  : bool
Validates if passed value is boolean-like.
compareFields()  : bool
Compare one field to another.
compareWith()  : bool
Compare one field to another.
comparison()  : bool
Used to compare 2 numeric values.
containsNonAlphaNumeric()  : bool
Checks if a string contains one or more non-alphanumeric characters.
creditCard()  : bool
Validation of credit card numbers.
custom()  : bool
Used when a custom regular expression is needed.
date()  : bool
Date validation, determines if the string passed is a valid date.
datetime()  : bool
Validates a datetime value
decimal()  : bool
Checks that a value is a valid decimal. Both the sign and exponent are optional.
email()  : bool
Validates for an email address.
equalTo()  : bool
Checks that value is exactly $comparedTo.
extension()  : bool
Checks that value has a valid file extension.
falsey()  : bool
Validates if given value is falsey.
fileSize()  : bool
Checks the filesize
geoCoordinate()  : bool
Validates a geographic coordinate.
hexColor()  : bool
Check that the input value is a 6 digits hex color.
iban()  : 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
imageHeight()  : bool
Validates the image height.
imageSize()  : bool
Validates the size of an uploaded image.
imageWidth()  : bool
Validates the image width.
inList()  : bool
Checks if a value is in a given list. Comparison is case sensitive by default.
ip()  : bool
Validation of an IP address.
isArray()  : bool
Check that the input value is an array.
isInteger()  : bool
Check that the input value is an integer
iso8601()  : 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()
isScalar()  : bool
Check that the input value is a scalar.
latitude()  : bool
Convenience method for latitude validation.
lengthBetween()  : bool
Checks that a string length is within specified range.
localizedTime()  : bool
Date and/or time string validation.
longitude()  : bool
Convenience method for longitude validation.
luhn()  : bool
Luhn algorithm
maxLength()  : bool
Checks whether the length of a string (in characters) is smaller or equal to a maximal length.
maxLengthBytes()  : bool
Checks whether the length of a string (in bytes) is smaller or equal to a maximal length.
mimeType()  : bool
Checks the mime type of a file.
minLength()  : bool
Checks whether the length of a string (in characters) is greater or equal to a minimal length.
minLengthBytes()  : bool
Checks whether the length of a string (in bytes) is greater or equal to a minimal length.
money()  : bool
Checks that a value is a monetary amount.
multiple()  : bool
Validates a multiple select. Comparison is case sensitive by default.
naturalNumber()  : bool
Checks if a value is a natural number.
notAlphaNumeric()  : bool
Checks that a doesn't contain any alpha numeric characters
notAsciiAlphaNumeric()  : bool
Checks that a doesn't contain any non-ascii alpha numeric characters
notBlank()  : bool
Checks that a string contains something other than whitespace
numElements()  : bool
Used to check the count of a given value of type array or Countable.
numeric()  : bool
Checks if a value is numeric.
range()  : bool
Validates that a number is in specified range.
time()  : bool
Time validation, determines if the string passed is a valid time.
truthy()  : bool
Validates if given value is truthy.
uploadedFile()  : bool
Validate an uploaded file.
uploadError()  : bool
Checking for upload errors
url()  : bool
Checks that a value is a valid URL according to https://www.w3.org/Addressing/URL/url-spec.txt
utf8()  : bool
Check that the input value is a utf8 string.
uuid()  : bool
Checks that a value is a valid UUID - https://tools.ietf.org/html/rfc4122
_check()  : bool
Runs a regular expression match.
_getDateString()  : string
Converts an array representing a date or datetime into a ISO string.
_populateIp()  : void
Lazily populate the IP address patterns used for validations
_reset()  : void
Reset internal variables for another validation run.
getFilename()  : string|false
Helper for reading the file out of the various file implementations we accept.

Constants

COMPARE_EQUAL

Equal to comparison operator.

public string COMPARE_EQUAL = '=='

COMPARE_GREATER

Greater than comparison operator.

public string COMPARE_GREATER = '>'

COMPARE_GREATER_OR_EQUAL

Greater than or equal to comparison operator.

public string COMPARE_GREATER_OR_EQUAL = '>='

COMPARE_LESS

Less than comparison operator.

public string COMPARE_LESS = '<'

COMPARE_LESS_OR_EQUAL

Less than or equal to comparison operator.

public string COMPARE_LESS_OR_EQUAL = '<='

COMPARE_NOT_EQUAL

Not equal to comparison operator.

public string COMPARE_NOT_EQUAL = '!='

COMPARE_NOT_SAME

Not same as comparison operator.

public string COMPARE_NOT_SAME = '!=='

COMPARE_SAME

Same as operator.

public string COMPARE_SAME = '==='

DATETIME_ISO8601

Datetime ISO8601 format

public string DATETIME_ISO8601 = 'iso8601'

DEFAULT_LOCALE

Default locale

public string DEFAULT_LOCALE = 'en_US'

COMPARE_STRING

protected array<string|int, string> COMPARE_STRING = [self::COMPARE_EQUAL, self::COMPARE_NOT_EQUAL, self::COMPARE_SAME, self::COMPARE_NOT_SAME]

Properties

$errors

Holds an array of errors messages set in this class.

public static array<string|int, mixed> $errors = []

These are used for debugging purposes

$_pattern

Some complex patterns needed in multiple places

protected static array<string, string> $_pattern = ['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+)?)']

Methods

alphaNumeric()

Checks that a string contains only integer or letters.

public static alphaNumeric(mixed $check) : bool

This method's definition of letters and integers includes unicode characters. Use asciiAlphaNumeric() if you want to exclude unicode.

Parameters
$check : mixed

Value to check

Return values
bool

Success

ascii()

Check that the input value is within the ascii byte range.

public static ascii(mixed $value) : bool

This method will reject all non-string values.

Parameters
$value : mixed

The value to check

Return values
bool

asciiAlphaNumeric()

Checks that a string contains only ascii integer or letters.

public static asciiAlphaNumeric(mixed $check) : bool
Parameters
$check : mixed

Value to check

Return values
bool

Success

boolean()

Validates if passed value is boolean-like.

public static boolean(string|int|bool $check[, array<string|int, string|int|bool> $booleanValues = [] ]) : bool

The list of what is considered to be boolean values, may be set via $booleanValues.

Parameters
$check : string|int|bool

Value to check.

$booleanValues : array<string|int, string|int|bool> = []

List of valid boolean values, defaults to [true, false, 0, 1, '0', '1'].

Return values
bool

Success.

compareFields()

Compare one field to another.

public static compareFields(mixed $check, string $field, string $operator, array<string, mixed> $context) : bool

Return true if the comparison matches the expected result.

Parameters
$check : mixed

The value to find in $field.

$field : string

The field to check $check against. This field must be present in $context.

$operator : string

Comparison operator. See Validation::comparison().

$context : array<string, mixed>

The validation context.

Tags
since
3.6.0
Return values
bool

compareWith()

Compare one field to another.

public static compareWith(mixed $check, string $field, array<string, mixed> $context) : bool

If both fields have exactly the same value this method will return true.

Parameters
$check : mixed

The value to find in $field.

$field : string

The field to check $check against. This field must be present in $context.

$context : array<string, mixed>

The validation context.

Return values
bool

comparison()

Used to compare 2 numeric values.

public static comparison(string|int $check1, string $operator, string|int $check2) : bool
Parameters
$check1 : string|int

The left value to compare.

$operator : string

Can be one of following operator strings: '>', '<', '>=', '<=', '==', '!=', '===' and '!=='. You can use one of the Validation::COMPARE_* constants.

$check2 : string|int

The right value to compare.

Return values
bool

Success

containsNonAlphaNumeric()

Checks if a string contains one or more non-alphanumeric characters.

public static containsNonAlphaNumeric(mixed $check[, int $count = 1 ]) : bool

Use instead. Will be removed in 5.0

Returns true if string contains at least the specified number of non-alphanumeric characters

Parameters
$check : mixed

Value to check

$count : int = 1

Number of non-alphanumerics to check for

Return values
bool

Success

creditCard()

Validation of credit card numbers.

public static creditCard(mixed $check[, array<string|int, string>|string $type = 'fast' ][, bool $deep = false ][, string|null $regex = null ]) : bool

Returns true if $check is in the proper credit card format.

Parameters
$check : mixed

credit card number to validate

$type : array<string|int, string>|string = '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 : bool = false

set to true this will check the Luhn algorithm of the credit card.

$regex : string|null = null

A custom regex, this will be used instead of the defined regex values.

Tags
see
Validation::luhn()
Return values
bool

Success

custom()

Used when a custom regular expression is needed.

public static custom(mixed $check[, string|null $regex = null ]) : bool
Parameters
$check : mixed

The value to check.

$regex : string|null = null

If $check is passed as a string, $regex must also be set to valid regular expression

Return values
bool

Success

date()

Date validation, determines if the string passed is a valid date.

public static date(mixed $check[, array<string|int, string>|string $format = 'ymd' ][, string|null $regex = null ]) : bool

keys that expect full month, day and year will validate leap years.

Years are valid from 0001 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
$check : mixed

a valid date string/object

$format : array<string|int, string>|string = 'ymd'

Use a string or an array of the keys above. Arrays should be passed as ['dmy', 'mdy', ...]

$regex : string|null = null

If a custom regular expression is used this is the only validation that will occur.

Return values
bool

Success

datetime()

Validates a datetime value

public static datetime(mixed $check[, array<string|int, mixed>|string $dateFormat = 'ymd' ][, string|null $regex = null ]) : bool

All values matching the "date" core validation rule, and the "time" one will be valid

Parameters
$check : mixed

Value to check

$dateFormat : array<string|int, mixed>|string = 'ymd'

Format of the date part. See Validation::date() for more information. Or Validation::DATETIME_ISO8601 to validate an ISO8601 datetime value.

$regex : string|null = null

Regex for the date part. If a custom regular expression is used this is the only validation that will occur.

Tags
see
Validation::date()
see
Validation::time()
Return values
bool

True if the value is valid, false otherwise

decimal()

Checks that a value is a valid decimal. Both the sign and exponent are optional.

public static decimal(mixed $check[, int|true|null $places = null ][, string|null $regex = null ]) : bool

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
$check : mixed

The value the test for decimal.

$places : int|true|null = null

Decimal places.

$regex : string|null = null

If a custom regular expression is used, this is the only validation that will occur.

Return values
bool

Success

email()

Validates for an email address.

public static email(mixed $check[, bool $deep = false ][, string|null $regex = null ]) : bool

Only uses getmxrr() checking for deep validation, or any PHP version on a non-windows distribution

Parameters
$check : mixed

Value to check

$deep : bool = false

Perform a deeper validation (if true), by also checking availability of host

$regex : string|null = null

Regex to use (if none it will use built in regex)

Return values
bool

Success

equalTo()

Checks that value is exactly $comparedTo.

public static equalTo(mixed $check, mixed $comparedTo) : bool
Parameters
$check : mixed

Value to check

$comparedTo : mixed

Value to compare

Return values
bool

Success

extension()

Checks that value has a valid file extension.

public static extension(UploadedFileInterface|array<string|int, mixed>|string $check[, array<string|int, string> $extensions = ['gif', 'jpeg', 'png', 'jpg'] ]) : bool
Parameters
$check : UploadedFileInterface|array<string|int, mixed>|string

Value to check

$extensions : array<string|int, string> = ['gif', 'jpeg', 'png', 'jpg']

file extensions to allow. By default extensions are 'gif', 'jpeg', 'png', 'jpg'

Return values
bool

Success

falsey()

Validates if given value is falsey.

public static falsey(string|int|bool $check[, array<string|int, string|int|bool> $falseyValues = [] ]) : bool

The list of what is considered to be falsey values, may be set via $falseyValues.

Parameters
$check : string|int|bool

Value to check.

$falseyValues : array<string|int, string|int|bool> = []

List of valid falsey values, defaults to [false, 0, '0'].

Return values
bool

Success.

fileSize()

Checks the filesize

public static fileSize(UploadedFileInterface|array<string|int, mixed>|string $check, string $operator, string|int $size) : bool

Will check the filesize of files/UploadedFileInterface instances by checking the filesize() on disk and not relying on the length reported by the client.

Parameters
$check : UploadedFileInterface|array<string|int, mixed>|string

Value to check.

$operator : string

See Validation::comparison().

$size : string|int

Size in bytes or human readable string like '5MB'.

Return values
bool

Success

geoCoordinate()

Validates a geographic coordinate.

public static geoCoordinate(mixed $value[, array<string, mixed> $options = [] ]) : bool

Supported formats:

  • <latitude>, <longitude> Example: -25.274398, 133.775136

Options

  • 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.
Parameters
$value : mixed

Geographic location as string

$options : array<string, mixed> = []

Options for the validation logic.

Return values
bool

hexColor()

Check that the input value is a 6 digits hex color.

public static hexColor(mixed $check) : bool
Parameters
$check : mixed

The value to check

Return values
bool

Success

iban()

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

public static iban(mixed $check) : bool
Parameters
$check : mixed

The value to check

Return values
bool

Success

imageHeight()

Validates the image height.

public static imageHeight(mixed $file, string $operator, int $height) : bool
Parameters
$file : mixed

The uploaded file data from PHP.

$operator : string

Comparison operator.

$height : int

Min or max height.

Return values
bool

imageSize()

Validates the size of an uploaded image.

public static imageSize(mixed $file, array<string, mixed> $options) : bool
Parameters
$file : mixed

The uploaded file data from PHP.

$options : array<string, mixed>

Options to validate width and height.

Tags
throws
InvalidArgumentException
Return values
bool

imageWidth()

Validates the image width.

public static imageWidth(mixed $file, string $operator, int $width) : bool
Parameters
$file : mixed

The uploaded file data from PHP.

$operator : string

Comparison operator.

$width : int

Min or max width.

Return values
bool

inList()

Checks if a value is in a given list. Comparison is case sensitive by default.

public static inList(mixed $check, array<string|int, string> $list[, bool $caseInsensitive = false ]) : bool
Parameters
$check : mixed

Value to check.

$list : array<string|int, string>

List to check against.

$caseInsensitive : bool = false

Set to true for case insensitive comparison.

Return values
bool

Success.

ip()

Validation of an IP address.

public static ip(mixed $check[, string $type = 'both' ]) : bool
Parameters
$check : mixed

The string to test.

$type : string = 'both'

The IP Protocol version to validate against

Return values
bool

Success

isArray()

Check that the input value is an array.

public static isArray(mixed $value) : bool
Parameters
$value : mixed

The value to check

Return values
bool

isInteger()

Check that the input value is an integer

public static isInteger(mixed $value) : bool

This method will accept strings that contain only integer data as well.

Parameters
$value : mixed

The value to check

Return values
bool

iso8601()

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()

public static iso8601(mixed $check) : bool
Parameters
$check : mixed

Value to check

Tags
see
Regex

credits: https://www.myintervals.com/blog/2009/05/20/iso-8601-date-validation-that-doesnt-suck/

Return values
bool

True if the value is valid, false otherwise

isScalar()

Check that the input value is a scalar.

public static isScalar(mixed $value) : bool

This method will accept integers, floats, strings and booleans, but not accept arrays, objects, resources and nulls.

Parameters
$value : mixed

The value to check

Return values
bool

lengthBetween()

Checks that a string length is within specified range.

public static lengthBetween(mixed $check, int $min, int $max) : bool

Spaces are included in the character count. Returns true if string matches value min, max, or between min and max,

Parameters
$check : mixed

Value to check for length

$min : int

Minimum value in range (inclusive)

$max : int

Maximum value in range (inclusive)

Return values
bool

Success

localizedTime()

Date and/or time string validation.

public static localizedTime(mixed $check[, string $type = 'datetime' ][, string|int|null $format = null ]) : bool

Uses I18n::Time to parse the date. This means parsing is locale dependent.

Parameters
$check : mixed

a date string or object (will always pass)

$type : string = 'datetime'

Parser type, one out of 'date', 'time', and 'datetime'

$format : string|int|null = null

any format accepted by IntlDateFormatter

Tags
throws
InvalidArgumentException

when unsupported $type given

see
Time::parseDate()
see
Time::parseTime()
see
Time::parseDateTime()
Return values
bool

Success

maxLength()

Checks whether the length of a string (in characters) is smaller or equal to a maximal length.

public static maxLength(mixed $check, int $max) : bool
Parameters
$check : mixed

The string to test

$max : int

The maximal string length

Return values
bool

Success

maxLengthBytes()

Checks whether the length of a string (in bytes) is smaller or equal to a maximal length.

public static maxLengthBytes(mixed $check, int $max) : bool
Parameters
$check : mixed

The string to test

$max : int

The maximal string length

Return values
bool

Success

mimeType()

Checks the mime type of a file.

public static mimeType(UploadedFileInterface|array<string|int, mixed>|string $check[, array<string|int, mixed>|string $mimeTypes = [] ]) : bool

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.

Parameters
$check : UploadedFileInterface|array<string|int, mixed>|string

Value to check.

$mimeTypes : array<string|int, mixed>|string = []

Array of mime types or regex pattern to check.

Tags
throws
RuntimeException

when mime type can not be determined.

throws
LogicException

when ext/fileinfo is missing

Return values
bool

Success

minLength()

Checks whether the length of a string (in characters) is greater or equal to a minimal length.

public static minLength(mixed $check, int $min) : bool
Parameters
$check : mixed

The string to test

$min : int

The minimal string length

Return values
bool

Success

minLengthBytes()

Checks whether the length of a string (in bytes) is greater or equal to a minimal length.

public static minLengthBytes(mixed $check, int $min) : bool
Parameters
$check : mixed

The string to test

$min : int

The minimal string length (in bytes)

Return values
bool

Success

money()

Checks that a value is a monetary amount.

public static money(mixed $check[, string $symbolPosition = 'left' ]) : bool
Parameters
$check : mixed

Value to check

$symbolPosition : string = 'left'

Where symbol is located (left/right)

Return values
bool

Success

multiple()

Validates a multiple select. Comparison is case sensitive by default.

public static multiple(mixed $check[, array<string, mixed> $options = [] ][, bool $caseInsensitive = false ]) : bool

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
$check : mixed

Value to check

$options : array<string, mixed> = []

Options for the check.

$caseInsensitive : bool = false

Set to true for case insensitive comparison.

Return values
bool

Success

naturalNumber()

Checks if a value is a natural number.

public static naturalNumber(mixed $check[, bool $allowZero = false ]) : bool
Parameters
$check : mixed

Value to check

$allowZero : bool = false

Set true to allow zero, defaults to false

Tags
see
https://en.wikipedia.org/wiki/Natural_number
Return values
bool

Success

notAlphaNumeric()

Checks that a doesn't contain any alpha numeric characters

public static notAlphaNumeric(mixed $check) : bool

This method's definition of letters and integers includes unicode characters. Use notAsciiAlphaNumeric() if you want to exclude ascii only.

Parameters
$check : mixed

Value to check

Return values
bool

Success

notAsciiAlphaNumeric()

Checks that a doesn't contain any non-ascii alpha numeric characters

public static notAsciiAlphaNumeric(mixed $check) : bool
Parameters
$check : mixed

Value to check

Return values
bool

Success

notBlank()

Checks that a string contains something other than whitespace

public static notBlank(mixed $check) : bool

Returns true if string contains something other than whitespace

Parameters
$check : mixed

Value to check

Return values
bool

Success

numElements()

Used to check the count of a given value of type array or Countable.

public static numElements(mixed $check, string $operator, int $expectedCount) : bool
Parameters
$check : mixed

The value to check the count on.

$operator : string

Can be either a word or operand is greater >, is less <, greater or equal >= less or equal <=, is less <, equal to ==, not equal !=

$expectedCount : int

The expected count value.

Return values
bool

Success

numeric()

Checks if a value is numeric.

public static numeric(mixed $check) : bool
Parameters
$check : mixed

Value to check

Return values
bool

Success

range()

Validates that a number is in specified range.

public static range(mixed $check[, float|null $lower = null ][, float|null $upper = null ]) : bool

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
$check : mixed

Value to check

$lower : float|null = null

Lower limit

$upper : float|null = null

Upper limit

Return values
bool

Success

time()

Time validation, determines if the string passed is a valid time.

public static time(mixed $check) : bool

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.

Parameters
$check : mixed

a valid time string/object

Return values
bool

Success

truthy()

Validates if given value is truthy.

public static truthy(string|int|bool $check[, array<string|int, string|int|bool> $truthyValues = [] ]) : bool

The list of what is considered to be truthy values, may be set via $truthyValues.

Parameters
$check : string|int|bool

Value to check.

$truthyValues : array<string|int, string|int|bool> = []

List of valid truthy values, defaults to [true, 1, '1'].

Return values
bool

Success.

uploadedFile()

Validate an uploaded file.

public static uploadedFile(mixed $file[, array<string, mixed> $options = [] ]) : bool

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. 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.
Parameters
$file : mixed

The uploaded file data from PHP.

$options : array<string, mixed> = []

An array of options for the validation.

Return values
bool

url()

Checks that a value is a valid URL according to https://www.w3.org/Addressing/URL/url-spec.txt

public static url(mixed $check[, bool $strict = false ]) : bool

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 https://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) as defined in RFC 3986
Parameters
$check : mixed

Value to check

$strict : bool = false

Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)

Tags
link
https://tools.ietf.org/html/rfc3986
Return values
bool

Success

utf8()

Check that the input value is a utf8 string.

public static utf8(mixed $value[, array<string, mixed> $options = [] ]) : bool

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
$value : mixed

The value to check

$options : array<string, mixed> = []

An array of options. See above for the supported options.

Return values
bool

uuid()

Checks that a value is a valid UUID - https://tools.ietf.org/html/rfc4122

public static uuid(mixed $check) : bool
Parameters
$check : mixed

Value to check

Return values
bool

Success

_check()

Runs a regular expression match.

protected static _check(mixed $check, string $regex) : bool
Parameters
$check : mixed

Value to check against the $regex expression

$regex : string

Regular expression

Return values
bool

Success of match

_getDateString()

Converts an array representing a date or datetime into a ISO string.

protected static _getDateString(array<string, mixed> $value) : string

The arrays are typically sent for validation from a form generated by the CakePHP FormHelper.

Parameters
$value : array<string, mixed>

The array representing a date or datetime.

Return values
string

_populateIp()

Lazily populate the IP address patterns used for validations

protected static _populateIp() : void

_reset()

Reset internal variables for another validation run.

protected static _reset() : void

getFilename()

Helper for reading the file out of the various file implementations we accept.

protected static getFilename(mixed $check) : string|false
Parameters
$check : mixed

The data to read a filename out of.

Return values
string|false

Either the filename or false on failure.


        
On this page

Search results