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 3.0 Red Velvet API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 3.0
      • 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
    • Auth
    • Cache
    • Collection
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • I18n
    • Log
    • Network
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
    • Validation
    • View

Class Validation

Validation Class. Used for validation of model data

Offers different validation methods.

Namespace: Cake\Validation

Constants

  • string
    DEFAULT_LOCALE ¶
    'en_US'

    Default locale

Property Summary

  • $_pattern protected static
    array

    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.

  • _defaults() protected static

    Get the values to use when value sent to validation method is an array.

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

  • blank() public static deprecated

    Returns true if field is left blank -OR- only whitespace characters are present in its value Whitespace characters include Space, Tab, Carriage Return, Newline

  • boolean() public static

    Boolean validation, determines if value passed is a boolean integer or true/false.

  • cc() public static

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

  • compareWith() public static

    Compare one field to another.

  • comparison() public static

    Used to compare 2 numeric values.

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

  • equalTo() public static

    Checks that value is exactly $comparedTo.

  • extension() public static

    Checks that value has a valid file extension.

  • fileSize() public static

    Checks the filesize

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

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

  • luhn() public static

    Luhn algorithm

  • maxLength() public static

    Checks whether the length of a string 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 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.

  • notBlank() public static

    Checks that a string contains something other than whitespace

  • notEmpty() public static deprecated

    Backwards compatibility wrapper for Validation::notBlank().

  • 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) or am/pm ([H]H:MM[a|p]m) Does not allow/validate seconds.

  • 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 http://www.w3.org/Addressing/URL/url-spec.txt

  • userDefined() public static deprecated

    Runs an user-defined validation.

  • uuid() public static

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

Method Detail

_check() ¶ protected static

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

Runs a regular expression match.

Parameters
string $check

Value to check against the $regex expression

string $regex

Regular expression

Returns
bool

_defaults() ¶ protected static

_defaults(array $params): array

Get the values to use when value sent to validation method is an array.

Parameters
array $params

Parameters sent to validation method

Returns
array

_getDateString() ¶ protected static

_getDateString(array $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 $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(string|array $check): bool

Checks that a string contains only integer or letters

Returns true if string contains only integer or letters

$check can be passed as an array: ['check' => 'valueToCheck'];

Parameters
string|array $check

Value to check

Returns
bool

blank() ¶ public static

blank(string|array $check): bool

Returns true if field is left blank -OR- only whitespace characters are present in its value Whitespace characters include Space, Tab, Carriage Return, Newline

$check can be passed as an array: ['check' => 'valueToCheck'];

Parameters
string|array $check

Value to check

Returns
bool

boolean() ¶ public static

boolean(string $check): bool

Boolean validation, determines if value passed is a boolean integer or true/false.

Parameters
string $check

a valid boolean

Returns
bool

cc() ¶ public static

cc(string|array $check, string|array $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
string|array $check

credit card number to validate

string|array $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 can also be passed, this will be used instead of the defined regex values

Returns
bool
See Also
Validation::luhn()

compareWith() ¶ public static

compareWith(mixed $check, string $field, array $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 $context

The validation context.

Returns
bool

comparison() ¶ public static

comparison(string|array $check1, string $operator = null, int $check2 = null): bool

Used to compare 2 numeric values.

Parameters
string|array $check1

if string is passed for, a string must also be passed for $check2 used as an array it must be passed as ['check1' => value, 'operator' => 'value', 'check2' => value]

string $operator optional

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

int $check2 optional

only needed if $check1 is a string

Returns
bool

custom() ¶ public static

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

Used when a custom regular expression is needed.

Parameters
string|array $check

When used as a string, $regex must also be a valid regular expression. As and array: ['check' => value, 'regex' => 'valid regular expression']

string|null $regex optional

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

Returns
bool

date() ¶ public static

date(string|DateTime $check, string|array $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 1800 to 2999.

Formats:

  • dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash
  • mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
  • ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
  • dMy 27 December 2006 or 27 Dec 2006
  • Mdy December 27, 2006 or Dec 27, 2006 comma is optional
  • My December 2006 or Dec 2006
  • my 12/2006 or 12/06 separators can be a space, period, dash, forward slash
  • ym 2006/12 or 06/12 separators can be a space, period, dash, forward slash
  • y 2006 just the year without any separators
Parameters
string|DateTime $check

a valid date string/object

string|array $format optional

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

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(string|DateTime $check, string|array $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

Parameters
string|DateTime $check

Value to check

string|array $dateFormat optional

Format of the date part. See Validation::date for more information.

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
Validation::date
Validation::time

decimal() ¶ public static

decimal(float $check, int $places = null, string|null $regex = null): bool

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

Valid Places:

  • null => Any number of decimal places, including none. The '.' is not required.
  • true => Any number of decimal places greater than 0, or a float|double. The '.' is required.
  • 1..N => Exactly that many number of decimal places. The '.' is required.
Parameters
float $check

The value the test for decimal.

int $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(string $check, bool $deep = false, string $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
string $check

Value to check

bool $deep optional

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

string $regex optional

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

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(string|array $check, array $extensions = ['gif', 'jpeg', 'png', 'jpg']): bool

Checks that value has a valid file extension.

Parameters
string|array $check

Value to check

array $extensions optional

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

Returns
bool

fileSize() ¶ public static

fileSize(string|array $check, string|null $operator = null, int|string|null $size = null): bool

Checks the filesize

Parameters
string|array $check

Value to check.

string|null $operator optional

See Validation::comparison().

int|string|null $size optional

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

Returns
bool

inList() ¶ public static

inList(string $check, array $list, bool $caseInsensitive = false): bool

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

Parameters
string $check

Value to check.

array $list

List to check against.

bool $caseInsensitive optional

Set to true for case insensitive comparison.

Returns
bool

ip() ¶ public static

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

Validation of an IP address.

Parameters
string $check

The string to test.

string $type optional

The IP Protocol version to validate against

Returns
bool

lengthBetween() ¶ public static

lengthBetween(string $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
string $check

Value to check for length

int $min

Minimum value in range (inclusive)

int $max

Maximum value in range (inclusive)

Returns
bool

luhn() ¶ public static

luhn(string|array $check, bool $deep = false): bool

Luhn algorithm

Parameters
string|array $check

Value to check.

bool $deep optional

If true performs deep check.

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

maxLength() ¶ public static

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

Checks whether the length of a string is smaller or equal to a maximal length..

Parameters
string $check

The string to test

int $max

The maximal string length

Returns
bool

mimeType() ¶ public static

mimeType(string|array $check, array|string $mimeTypes = []): bool

Checks the mime type of a file.

Parameters
string|array $check

Value to check.

array|string $mimeTypes optional

Array of mime types or regex pattern to check.

Returns
bool
Throws
RuntimeException
when mime type can not be determined.
LogicException
when ext/fileinfo is missing

minLength() ¶ public static

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

Checks whether the length of a string is greater or equal to a minimal length.

Parameters
string $check

The string to test

int $min

The minimal string length

Returns
bool

money() ¶ public static

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

Checks that a value is a monetary amount.

Parameters
string $check

Value to check

string $symbolPosition optional

Where symbol is located (left/right)

Returns
bool

multiple() ¶ public static

multiple(array $check, array $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
array $check

Value to check

array $options optional

Options for the check.

bool $caseInsensitive optional

Set to true for case insensitive comparison.

Returns
bool

naturalNumber() ¶ public static

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

Checks if a value is a natural number.

Parameters
string $check

Value to check

bool $allowZero optional

Set true to allow zero, defaults to false

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

notBlank() ¶ public static

notBlank(string|array $check): bool

Checks that a string contains something other than whitespace

Returns true if string contains something other than whitespace

$check can be passed as an array: ['check' => 'valueToCheck'];

Parameters
string|array $check

Value to check

Returns
bool

notEmpty() ¶ public static

notEmpty(string|array $check): bool

Backwards compatibility wrapper for Validation::notBlank().

Parameters
string|array $check

Value to check.

Returns
bool
See Also
Validation::notBlank()

numeric() ¶ public static

numeric(string $check): bool

Checks if a value is numeric.

Parameters
string $check

Value to check

Returns
bool

range() ¶ public static

range(string $check, int|float|null $lower = null, int|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
string $check

Value to check

int|float|null $lower optional

Lower limit

int|float|null $upper optional

Upper limit

Returns
bool

time() ¶ public static

time(string|DateTime $check): bool

Time validation, determines if the string passed is a valid time. Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m) Does not allow/validate seconds.

Parameters
string|DateTime $check

a valid time string/object

Returns
bool

uploadError() ¶ public static

uploadError(string|array $check, bool $allowNoFile = false): bool

Checking for upload errors

Parameters
string|array $check

Value to check.

bool $allowNoFile optional

Set to true to allow UPLOAD_ERR_NO_FILE as a pass.

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

uploadedFile() ¶ public static

uploadedFile(array $file, array $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 or not this file is optional. Defaults to false. If true a missing file will pass the validator regardless of other constraints.
Parameters
array $file

The uploaded file data from PHP.

array $options optional

An array of options for the validation.

Returns
bool

url() ¶ public static

url(string $check, bool $strict = false): bool

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

The regex checks for the following component parts:

  • a valid, optional, scheme
  • a valid ip address OR a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt with an optional port number
  • an optional valid path
  • an optional query string (get parameters)
  • an optional fragment (anchor tag)
Parameters
string $check

Value to check

bool $strict optional

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

Returns
bool

userDefined() ¶ public static

userDefined(string|array $check, object $object, string $method, array|null $args = null): mixed

Runs an user-defined validation.

Parameters
string|array $check

value that will be validated in user-defined methods.

object $object

class that holds validation method

string $method

class method name for validation to run

array|null $args optional

arguments to send to method

Returns
mixed

uuid() ¶ public static

uuid(string $check): bool

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

Parameters
string $check

Value to check

Returns
bool

Property Detail

$_pattern ¶ protected static

Some complex patterns needed in multiple places

Type
array

$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