CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 5.1 Chiffon API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 5.1
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Form
    • Http
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class Validation

Validation Class. Used for validation of model data

Offers different validation methods.

Namespace: Cake\Validation

Constants

  • string
    COMPARE_EQUAL ¶
    '=='

    Equal to comparison operator.

  • string
    COMPARE_GREATER ¶
    '>'

    Greater than comparison operator.

  • string
    COMPARE_GREATER_OR_EQUAL ¶
    '>='

    Greater than or equal to comparison operator.

  • string
    COMPARE_LESS ¶
    '<'

    Less than comparison operator.

  • string
    COMPARE_LESS_OR_EQUAL ¶
    '<='

    Less than or equal to comparison operator.

  • string
    COMPARE_NOT_EQUAL ¶
    '!='

    Not equal to comparison operator.

  • string
    COMPARE_NOT_SAME ¶
    '!=='

    Not same as comparison operator.

  • string
    COMPARE_SAME ¶
    '==='

    Same as operator.

  • list<string>
    COMPARE_STRING ¶
    [self::COMPARE_EQUAL, self::COMPARE_NOT_EQUAL, self::COMPARE_SAME, self::COMPARE_NOT_SAME]
  • string
    DATETIME_ISO8601 ¶
    'iso8601'

    Datetime ISO8601 format

  • string
    DEFAULT_LOCALE ¶
    'en_US'

    Default locale

Property Summary

  • $_pattern protected static
    array<string, string>

    Some complex patterns needed in multiple places

  • $errors public static
    array

    Holds an array of errors messages set in this class. These are used for debugging purposes

Method Summary

  • _check() protected static

    Runs a regular expression match.

  • _getDateString() protected static

    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.

  • _populateIp() protected static

    Lazily populate the IP address patterns used for validations

  • _reset() protected static

    Reset internal variables for another validation run.

  • alphaNumeric() public static

    Checks that a string contains only integer or letters.

  • ascii() public static

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

  • asciiAlphaNumeric() public static

    Checks that a string contains only ascii integer or letters.

  • boolean() public static

    Validates if passed value is boolean-like.

  • checkEnum() protected static
  • compareFields() public static

    Compare one field to another.

  • compareWith() public static

    Compare one field to another.

  • comparison() public static

    Used to compare 2 numeric values.

  • creditCard() public static

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

  • custom() public static

    Used when a custom regular expression is needed.

  • date() public static

    Date validation, determines if the string passed is a valid date. keys that expect full month, day and year will validate leap years.

  • datetime() public static

    Validates a datetime value

  • decimal() public static

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

  • email() public static

    Validates for an email address.

  • enum() public static

    Checks that the value is a valid backed enum instance or value.

  • enumExcept() public static

    Checks that the value is a valid backed enum instance or value.

  • enumOnly() public static

    Checks that the value is a valid backed enum instance or value.

  • equalTo() public static

    Checks that value is exactly $comparedTo.

  • extension() public static

    Checks that value has a valid file extension.

  • falsey() public static

    Validates if given value is falsey.

  • fileSize() public static

    Checks the filesize

  • geoCoordinate() public static

    Validates a geographic coordinate.

  • getFilename() protected static

    Helper for reading the file name.

  • hexColor() public static

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

  • iban() public static

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

    Validates the image height.

  • imageSize() public static

    Validates the size of an uploaded image.

  • imageWidth() public static

    Validates the image width.

  • inList() public static

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

  • ip() public static

    Validation of an IP address.

  • isArray() public static

    Check that the input value is an array.

  • isInteger() public static

    Check that the input value is an integer

  • isScalar() public static

    Check that the input value is a scalar.

  • isValidEnum() protected static
  • iso8601() public static

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

  • latitude() public static

    Convenience method for latitude validation.

  • lengthBetween() public static

    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,

  • localizedTime() public static

    Date and/or time string validation. Uses I18n::Time to parse the date. This means parsing is locale dependent.

  • longitude() public static

    Convenience method for longitude validation.

  • luhn() public static

    Luhn algorithm

  • maxLength() public static

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

  • maxLengthBytes() public static

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

  • mimeType() public static

    Checks the mime type of a file.

  • minLength() public static

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

  • minLengthBytes() public static

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

  • money() public static

    Checks that a value is a monetary amount.

  • multiple() public static

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

  • naturalNumber() public static

    Checks if a value is a natural number.

  • notAlphaNumeric() public static

    Checks that a doesn't contain any alpha numeric characters

  • notAsciiAlphaNumeric() public static

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

  • notBlank() public static

    Checks that a string contains something other than whitespace

  • numElements() public static

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

  • numeric() public static

    Checks if a value is numeric.

  • range() public static

    Validates that a number is in specified range.

  • time() public static

    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)

  • truthy() public static

    Validates if given value is truthy.

  • uploadError() public static

    Checking for upload errors

  • uploadedFile() public static

    Validate an uploaded file.

  • url() public static

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

  • utf8() public static

    Check that the input value is a utf8 string.

  • uuid() public static

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

Method Detail

_check() ¶ protected static

_check(mixed $check, string $regex): bool

Runs a regular expression match.

Parameters
mixed $check

Value to check against the $regex expression

string $regex

Regular expression

Returns
bool

_getDateString() ¶ protected static

_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.

Parameters
array<string, mixed> $value

The array representing a date or datetime.

Returns
string

_populateIp() ¶ protected static

_populateIp(): void

Lazily populate the IP address patterns used for validations

Returns
void

_reset() ¶ protected static

_reset(): void

Reset internal variables for another validation run.

Returns
void

alphaNumeric() ¶ public static

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.

Parameters
mixed $check

Value to check

Returns
bool

ascii() ¶ public static

ascii(mixed $value): bool

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

This method will reject all non-string values.

Parameters
mixed $value

The value to check

Returns
bool

asciiAlphaNumeric() ¶ public static

asciiAlphaNumeric(mixed $check): bool

Checks that a string contains only ascii integer or letters.

Parameters
mixed $check

Value to check

Returns
bool

boolean() ¶ public static

boolean(mixed $check, array<string|int|bool> $booleanValues = [true, false, 0, 1, '0', '1']): bool

Validates if passed value is boolean-like.

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

Parameters
mixed $check

Value to check.

array<string|int|bool> $booleanValues optional

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

Returns
bool

checkEnum() ¶ protected static

checkEnum(mixed $check, class-string<BackedEnum> $enumClassName, array<string, mixed> $options = []): bool
Parameters
mixed $check
class-string<BackedEnum> $enumClassName
array<string, mixed> $options optional
Returns
bool

compareFields() ¶ public static

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.

Parameters
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.

Returns
bool

compareWith() ¶ public static

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.

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<string, mixed> $context

The validation context.

Returns
bool

comparison() ¶ public static

comparison(mixed $check1, string $operator, mixed $check2): bool

Used to compare 2 numeric values.

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

mixed $check2

The right value to compare.

Returns
bool

creditCard() ¶ public static

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.

Parameters
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.

Returns
bool
See Also
\Cake\Validation\Validation::luhn()

custom() ¶ public static

custom(mixed $check, string|null $regex = null): bool

Used when a custom regular expression is needed.

Parameters
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

Returns
bool

date() ¶ public static

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.

Formats:

  • ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
  • 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
  • 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
mixed $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.

Returns
bool

datetime() ¶ public static

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

Years are valid from 0001 to 2999.

Formats:

  • ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
    • 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
    • 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

Time is validated 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
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.

Returns
bool
See Also
\Cake\Validation\Validation::date()
\Cake\Validation\Validation::time()

decimal() ¶ public static

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.

Be aware that the currently set locale is being used to determine the decimal and thousands separator of the given number.

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
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.

Returns
bool

email() ¶ public static

email(mixed $check, bool|null $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

Parameters
mixed $check

Value to check

bool|null $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)

Returns
bool

enum() ¶ public static

enum(mixed $check, class-string<BackedEnum> $enumClassName): bool

Checks that the value is a valid backed enum instance or value.

Parameters
mixed $check

Value to check

class-string<BackedEnum> $enumClassName

The valid backed enum class name

Returns
bool

enumExcept() ¶ public static

enumExcept(mixed $check, array<BackedEnum> $cases): bool

Checks that the value is a valid backed enum instance or value.

Parameters
mixed $check

Value to check

array<BackedEnum> $cases

Array of enum cases that are not valid.

Returns
bool

enumOnly() ¶ public static

enumOnly(mixed $check, array<BackedEnum> $cases): bool

Checks that the value is a valid backed enum instance or value.

Parameters
mixed $check

Value to check

array<BackedEnum> $cases

Array of enum cases that are valid.

Returns
bool

equalTo() ¶ public static

equalTo(mixed $check, mixed $comparedTo): bool

Checks that value is exactly $comparedTo.

Parameters
mixed $check

Value to check

mixed $comparedTo

Value to compare

Returns
bool

extension() ¶ public static

extension(mixed $check, array<string> $extensions = ['gif', 'jpeg', 'png', 'jpg']): bool

Checks that value has a valid file extension.

Supports checking \Psr\Http\Message\UploadedFileInterface instances and and arrays with a name key.

Parameters
mixed $check

Value to check

array<string> $extensions optional

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

Returns
bool

falsey() ¶ public static

falsey(mixed $check, array<string|int|bool> $falseyValues = [false, 0, '0']): bool

Validates if given value is falsey.

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

Parameters
mixed $check

Value to check.

array<string|int|bool> $falseyValues optional

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

Returns
bool

fileSize() ¶ public static

fileSize(mixed $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.

Parameters
mixed $check

Value to check.

string $operator

See Validation::comparison().

string|int $size

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

Returns
bool

geoCoordinate() ¶ public static

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

Validates a geographic coordinate.

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

Geographic location as string

array<string, mixed> $options optional

Options for the validation logic.

Returns
bool

getFilename() ¶ protected static

getFilename(mixed $check): string|null

Helper for reading the file name.

Parameters
mixed $check

The data to read a filename out of.

Returns
string|null

hexColor() ¶ public static

hexColor(mixed $check): bool

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

Parameters
mixed $check

The value to check

Returns
bool

iban() ¶ public static

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

Parameters
mixed $check

The value to check

Returns
bool

imageHeight() ¶ public static

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

Validates the image height.

Parameters
mixed $file

The uploaded file data from PHP.

string $operator

Comparison operator.

int $height

Min or max height.

Returns
bool

imageSize() ¶ public static

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

Validates the size of an uploaded image.

Parameters
mixed $file

The uploaded file data from PHP.

array<string, mixed> $options

Options to validate width and height.

Returns
bool
Throws
InvalidArgumentException

imageWidth() ¶ public static

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

Validates the image width.

Parameters
mixed $file

The uploaded file data from PHP.

string $operator

Comparison operator.

int $width

Min or max width.

Returns
bool

inList() ¶ public static

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.

Parameters
mixed $check

Value to check.

array<string> $list

List to check against.

bool $caseInsensitive optional

Set to true for case-insensitive comparison.

Returns
bool

ip() ¶ public static

ip(mixed $check, string $type = 'both'): bool

Validation of an IP address.

Parameters
mixed $check

The string to test.

string $type optional

The IP Protocol version to validate against

Returns
bool

isArray() ¶ public static

isArray(mixed $value): bool

Check that the input value is an array.

Parameters
mixed $value

The value to check

Returns
bool

isInteger() ¶ public static

isInteger(mixed $value): bool

Check that the input value is an integer

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

Parameters
mixed $value

The value to check

Returns
bool

isScalar() ¶ public static

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.

Parameters
mixed $value

The value to check

Returns
bool

isValidEnum() ¶ protected static

isValidEnum(BackedEnum $enum, array<string, mixed> $options): bool
Parameters
BackedEnum $enum
array<string, mixed> $options
Returns
bool

iso8601() ¶ public static

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

Parameters
mixed $check

Value to check

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

latitude() ¶ public static

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

Convenience method for latitude validation.

Parameters
mixed $value

Latitude as string

array<string, mixed> $options optional

Options for the validation logic.

Returns
bool
See Also
\Cake\Validation\Validation::geoCoordinate()
Links
https://en.wikipedia.org/wiki/Latitude

lengthBetween() ¶ public static

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,

Parameters
mixed $check

Value to check for length

int $min

Minimum value in range (inclusive)

int $max

Maximum value in range (inclusive)

Returns
bool

localizedTime() ¶ public static

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.

Parameters
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

Returns
bool
Throws
InvalidArgumentException
when unsupported $type given
See Also
\Cake\I18n\Time::parseDate()
\Cake\I18n\Time::parseTime()
\Cake\I18n\Time::parseDateTime()

longitude() ¶ public static

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

Convenience method for longitude validation.

Parameters
mixed $value

Latitude as string

array<string, mixed> $options optional

Options for the validation logic.

Returns
bool
See Also
\Cake\Validation\Validation::geoCoordinate()
Links
https://en.wikipedia.org/wiki/Longitude

luhn() ¶ public static

luhn(mixed $check): bool

Luhn algorithm

Parameters
mixed $check

Value to check.

Returns
bool
See Also
https://en.wikipedia.org/wiki/Luhn_algorithm

maxLength() ¶ public static

maxLength(mixed $check, int $max): bool

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

Parameters
mixed $check

The string to test

int $max

The maximal string length

Returns
bool

maxLengthBytes() ¶ public static

maxLengthBytes(mixed $check, int $max): bool

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

Parameters
mixed $check

The string to test

int $max

The maximal string length

Returns
bool

mimeType() ¶ public static

mimeType(mixed $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.

Parameters
mixed $check

Value to check.

array|string $mimeTypes optional

Array of mime types or regex pattern to check.

Returns
bool
Throws
Cake\Core\Exception\CakeException
when mime type can not be determined.

minLength() ¶ public static

minLength(mixed $check, int $min): bool

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

Parameters
mixed $check

The string to test

int $min

The minimal string length

Returns
bool

minLengthBytes() ¶ public static

minLengthBytes(mixed $check, int $min): bool

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

Parameters
mixed $check

The string to test

int $min

The minimal string length (in bytes)

Returns
bool

money() ¶ public static

money(mixed $check, string $symbolPosition = 'left'): bool

Checks that a value is a monetary amount.

Parameters
mixed $check

Value to check

string $symbolPosition optional

Where symbol is located (left/right)

Returns
bool

multiple() ¶ public static

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

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

Value to check

array<string, mixed> $options optional

Options for the check.

bool $caseInsensitive optional

Set to true for case insensitive comparison.

Returns
bool

naturalNumber() ¶ public static

naturalNumber(mixed $check, bool $allowZero = false): bool

Checks if a value is a natural number.

Parameters
mixed $check

Value to check

bool $allowZero optional

Set true to allow zero, defaults to false

Returns
bool
See Also
https://en.wikipedia.org/wiki/Natural_number

notAlphaNumeric() ¶ public static

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.

Parameters
mixed $check

Value to check

Returns
bool

notAsciiAlphaNumeric() ¶ public static

notAsciiAlphaNumeric(mixed $check): bool

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

Parameters
mixed $check

Value to check

Returns
bool

notBlank() ¶ public static

notBlank(mixed $check): bool

Checks that a string contains something other than whitespace

Returns true if string contains something other than whitespace

Parameters
mixed $check

Value to check

Returns
bool

numElements() ¶ public static

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

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

Parameters
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.

Returns
bool

numeric() ¶ public static

numeric(mixed $check): bool

Checks if a value is numeric.

Parameters
mixed $check

Value to check

Returns
bool

range() ¶ public static

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.

Parameters
mixed $check

Value to check

float|null $lower optional

Lower limit

float|null $upper optional

Upper limit

Returns
bool

time() ¶ public static

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.

Parameters
mixed $check

a valid time string/object

Returns
bool

truthy() ¶ public static

truthy(mixed $check, array<string|int|bool> $truthyValues = [true, 1, '1']): bool

Validates if given value is truthy.

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

Parameters
mixed $check

Value to check.

array<string|int|bool> $truthyValues optional

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

Returns
bool

uploadError() ¶ public static

uploadError(mixed $check, bool $allowNoFile = false): bool

Checking for upload errors

Supports checking \Psr\Http\Message\UploadedFileInterface instances and and arrays with a error key.

Parameters
mixed $check

Value to check.

bool $allowNoFile optional

Set to true to allow UPLOAD_ERR_NO_FILE as a pass.

Returns
bool
See Also
https://secure.php.net/manual/en/features.file-upload.errors.php

uploadedFile() ¶ public static

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

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

The uploaded file data from PHP.

array<string, mixed> $options optional

An array of options for the validation.

Returns
bool

url() ¶ public static

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:

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

Returns
bool
Links
https://tools.ietf.org/html/rfc3986

utf8() ¶ public static

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.

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

The value to check

array<string, mixed> $options optional

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

Returns
bool

uuid() ¶ public static

uuid(mixed $check): bool

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

Parameters
mixed $check

Value to check

Returns
bool

Property Detail

$_pattern ¶ protected static

Some complex patterns needed in multiple places

Type
array<string, string>

$errors ¶ public static

Holds an array of errors messages set in this class. These are used for debugging purposes

Type
array
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs