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
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 4.2
      • 4.1
      • 4.0
      • 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

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
    • Network
      • Email
      • Http
    • Routing
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CakeNumber
  • CakeTime
  • ClassRegistry
  • Debugger
  • File
  • Folder
  • Inflector
  • ObjectCollection
  • Sanitize
  • Security
  • Set
  • String
  • Validation
  • Xml

Class Validation

Offers different validation methods.

Package: Cake\Utility
Since: CakePHP v 1.2.0.3830
Copyright: Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
License: License (http://www.opensource.org/licenses/mit-license.php)
Location: Cake/Utility/Validation.php

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

  • _pass() protected static

    Attempts to pass unhandled Validation locales to a class starting with $classPrefix and ending with Validation. For example $classPrefix = 'nl', the class would be NlValidation.

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

    Checks that a string length is within s specified range. Spaces are included in the character count. Returns true is string matches value min, max, or between min and max,

  • blank() public static

    Returns true if field is left blank -OR- only whitespace characters are present in it's 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.

  • 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 All values matching the "date" core validation rule, and the "time" one will be valid

  • decimal() public static

    Checks that a value is a valid decimal. If $places is null, the $check is allowed to be a scientific float If no decimal point is found a false will be returned. Both the sign and exponent are optional.

  • email() public static
    Validates for an email address.
  • equalTo() public static
    Check that value is exactly $comparedTo.
  • extension() public static
    Check that value has a valid file extension.
  • inList() public static
    Checks if a value is in a given list.
  • ip() public static
    Validation of an IP address.
  • luhn() public static
    Luhn algorithm
  • maxLength() public static
    Checks whether the length of a string is smaller or equal to a maximal length..
  • 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
    Validate a multiple select.
  • notEmpty() public static
    Checks that a string contains something other than whitespace
  • numeric() public static
    Checks if a value is numeric.
  • phone() public static
    Check that a value is a valid phone number.
  • postal() public static
    Checks that a given value is a valid postal code.
  • range() public static

    Validate that a number is in specified range. if $lower and $upper are not set, will return true if $check is a legal finite on this platform

  • ssn() public static
    Checks that a value is a valid Social Security Number.
  • 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.

  • 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
    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( mixed $check , string $regex )

Runs a regular expression match.

Parameters
mixed $check
Value to check against the $regex expression
string $regex
Regular expression
Returns
boolean
Success of match

_defaults() protected static ¶

_defaults( array $params )

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

Parameters
array $params
Parameters sent to validation method

_pass() protected static ¶

_pass( string $method , mixed $check , string $classPrefix )

Attempts to pass unhandled Validation locales to a class starting with $classPrefix and ending with Validation. For example $classPrefix = 'nl', the class would be NlValidation.

Parameters
string $method
The method to call on the other class.
mixed $check
The value to check or an array of parameters for the method to be called.
string $classPrefix
The prefix for the class to do the validation.
Returns
mixed
Return of Passed method, false on failure

_populateIp() protected static ¶

_populateIp( )

Lazily populate the IP address patterns used for validations

_reset() protected static ¶

_reset( )

Reset internal variables for another validation run.

alphaNumeric() public static ¶

alphaNumeric( mixed $check )

Checks that a string contains only integer or letters

Returns true if string contains only integer or letters

$check can be passed as an array: array('check' => 'valueToCheck');

Parameters
mixed $check
Value to check
Returns
boolean
Success

between() public static ¶

between( string $check , integer $min , integer $max )

Checks that a string length is within s specified range. Spaces are included in the character count. Returns true is string matches value min, max, or between min and max,

Parameters
string $check
Value to check for length
integer $min
Minimum value in range (inclusive)
integer $max
Maximum value in range (inclusive)
Returns
boolean
Success

blank() public static ¶

blank( mixed $check )

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

$check can be passed as an array: array('check' => 'valueToCheck');

Parameters
mixed $check
Value to check
Returns
boolean
Success

boolean() public static ¶

boolean( string $check )

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

Parameters
string $check
a valid boolean
Returns
boolean
Success

cc() public static ¶

cc( mixed $check , mixed $type = 'fast' , boolean $deep = false , string $regex = null )

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

Parameters
mixed $check
credit card number to validate
mixed $type optional 'fast'

'all' may be passed as a sting, 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: array('amex', 'bankcard', 'maestro')

boolean $deep optional false
set to true this will check the Luhn algorithm of the credit card.
string $regex optional null
A custom regex can also be passed, this will be used instead of the defined regex values
Returns
boolean
Success
See
Validation::luhn()

comparison() public static ¶

comparison( mixed $check1 , string $operator = null , integer $check2 = null )

Used to compare 2 numeric values.

Parameters
mixed $check1

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

string $operator optional null

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

integer $check2 optional null
only needed if $check1 is a string
Returns
boolean
Success

custom() public static ¶

custom( mixed $check , string $regex = null )

Used when a custom regular expression is needed.

Parameters
mixed $check

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

string $regex optional null
If $check is passed as a string, $regex must also be set to valid regular expression
Returns
boolean
Success

date() public static ¶

date( string $check , mixed $format = 'ymd' , string $regex = null )

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

Parameters
string $check
a valid date string
mixed $format optional 'ymd'

Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc) Keys: 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 separators can be a space, period, dash, forward slash

string $regex optional null
If a custom regular expression is used this is the only validation that will occur.
Returns
boolean
Success

datetime() public static ¶

datetime( array $check , mixed $dateFormat = 'ymd' , string $regex = null )

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

Parameters
array $check
Value to check
mixed $dateFormat optional 'ymd'

Format of the date part Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc)

Keys:

  • 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 separators can be a space, period, dash, forward slash
string $regex optional null
Regex for the date part. If a custom regular expression is used this is the only validation that will occur.
Returns
boolean
True if the value is valid, false otherwise
See
Validation::date
Validation::time

decimal() public static ¶

decimal( integer $check , integer $places = null , string $regex = null )

Checks that a value is a valid decimal. If $places is null, the $check is allowed to be a scientific float If no decimal point is found a false will be returned. Both the sign and exponent are optional.

Parameters
integer $check
The value the test for decimal
integer $places optional null
if set $check value must have exactly $places after the decimal point
string $regex optional null
If a custom regular expression is used this is the only validation that will occur.
Returns
boolean
Success

email() public static ¶

email( string $check , boolean $deep = false , string $regex = null )

Validates for an email address.

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

Parameters
string $check
Value to check
boolean $deep optional false
Perform a deeper validation (if true), by also checking availability of host
string $regex optional null
Regex to use (if none it will use built in regex)
Returns
boolean
Success

equalTo() public static ¶

equalTo( mixed $check , mixed $comparedTo )

Check that value is exactly $comparedTo.

Parameters
mixed $check
Value to check
mixed $comparedTo
Value to compare
Returns
boolean
Success

extension() public static ¶

extension( mixed $check , array $extensions = array('gif', 'jpeg', 'png', 'jpg') )

Check that value has a valid file extension.

Parameters
mixed $check
Value to check
array $extensions optional array('gif', 'jpeg', 'png', 'jpg')
file extensions to allow
Returns
boolean
Success

inList() public static ¶

inList( string $check , array $list )

Checks if a value is in a given list.

Parameters
string $check
Value to check
array $list
List to check against
Returns
boolean
Success

ip() public static ¶

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

Validation of an IP address.

Parameters
string $check
The string to test.
string $type optional 'both'
The IP Protocol version to validate against
Returns
boolean
Success

luhn() public static ¶

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

Luhn algorithm

Parameters
string|array $check
boolean $deep optional false
Returns
boolean
Success
See
http://en.wikipedia.org/wiki/Luhn_algorithm

maxLength() public static ¶

maxLength( string $check , integer $max )

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

Parameters
string $check
The string to test
integer $max
The maximal string length
Returns
boolean
Success

minLength() public static ¶

minLength( string $check , integer $min )

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

Parameters
string $check
The string to test
integer $min
The minimal string length
Returns
boolean
Success

money() public static ¶

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

Checks that a value is a monetary amount.

Parameters
string $check
Value to check
string $symbolPosition optional 'left'
Where symbol is located (left/right)
Returns
boolean
Success

multiple() public static ¶

multiple( mixed $check , mixed $options = array() )

Validate a multiple select.

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
mixed $options optional array()
Options for the check.
Returns
boolean
Success

notEmpty() public static ¶

notEmpty( mixed $check )

Checks that a string contains something other than whitespace

Returns true if string contains something other than whitespace

$check can be passed as an array: array('check' => 'valueToCheck');

Parameters
mixed $check
Value to check
Returns
boolean
Success

numeric() public static ¶

numeric( string $check )

Checks if a value is numeric.

Parameters
string $check
Value to check
Returns
boolean
Success

phone() public static ¶

phone( mixed $check , string $regex = null , string $country = 'all' )

Check that a value is a valid phone number.

Parameters
mixed $check
Value to check (string or array)
string $regex optional null
Regular expression to use
string $country optional 'all'
Country code (defaults to 'all')
Returns
boolean
Success

postal() public static ¶

postal( mixed $check , string $regex = null , string $country = 'us' )

Checks that a given value is a valid postal code.

Parameters
mixed $check
Value to check
string $regex optional null
Regular expression to use
string $country optional 'us'
Country to use for formatting
Returns
boolean
Success

range() public static ¶

range( string $check , integer $lower = null , integer $upper = null )

Validate that a number is in specified range. if $lower and $upper are not set, will return true if $check is a legal finite on this platform

Parameters
string $check
Value to check
integer $lower optional null
Lower limit
integer $upper optional null
Upper limit
Returns
boolean
Success

ssn() public static ¶

ssn( mixed $check , string $regex = null , string $country = null )

Checks that a value is a valid Social Security Number.

Parameters
mixed $check
Value to check
string $regex optional null
Regular expression to use
string $country optional null
Country
Returns
boolean
Success

time() public static ¶

time( string $check )

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

Parameters
string $check
a valid time string
Returns
boolean
Success

url() public static ¶

url( string $check , boolean $strict = false )

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

The regex checks for the following component parts:

  • 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
boolean $strict optional false
Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher)
Returns
boolean
Success

userDefined() public static ¶

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

Runs an user-defined validation.

Parameters
mixed $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 $args optional null
arguments to send to method
Returns
mixed
user-defined class class method returns

uuid() public static ¶

uuid( string $check )

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

Parameters
string $check
Value to check
Returns
boolean
Success

Properties detail

$_pattern ¶

protected static array

Some complex patterns needed in multiple places

array(
    'hostname' => '(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)'
)

$errors ¶

public static array

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

array()
OpenHub
Rackspace
Rackspace
  • 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
  • Slack
  • Paid Support

Generated using CakePHP API Docs