Class Validation
Offers different validation methods.
- Object
- Validation
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: validation.php
Properties summary
-
$__pattern
publicarray
Some complex patterns needed in multiple places -
$check
publicstring
Set the value of methods $check param. -
$country
publicstring
Some class methods use a country to determine proper validation. This can be passed to methods in the $country param
-
$deep
publicstring
Some class methods use a deeper validation when set to true -
$errors
publicarray
Holds an array of errors messages set in this class. These are used for debugging purposes
-
$regex
publicstring
Set to a valid regular expression in the class methods. Can be set from $regex param also
-
$type
publicstring
Some class methods use the $type param to determine which validation to perfom in the method
Method Summary
-
__populateIp() public
-
__reset() public
Reset internal variables for another validation run. -
_check() public
Runs a regular expression match. -
_extract() public
Get the values to use when value sent to validation method is an array.
-
_ipv4() public
Validation of IPv4 addresses. -
_ipv6() public
Validation of IPv6 addresses. -
_luhn() public
Luhn algorithm -
_pass() public
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
. -
alphaNumeric() public
Checks that a string contains only integer or letters -
between() public
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
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
Boolean validation, determines if value passed is a boolean integer or true/false. -
cc() public
Validation of credit card numbers. Returns true if $check is in the proper credit card format.
-
comparison() public
Used to compare 2 numeric values. -
custom() public
Used when a custom regular expression is needed. -
date() public
Date validation, determines if the string passed is a valid date. keys that expect full month, day and year will validate leap years
-
decimal() public
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
Validates for an email address. -
equalTo() public
Check that value is exactly $comparedTo. -
extension() public
Check that value has a valid file extension. -
getInstance() public
Gets a reference to the Validation object instance -
inList() public
Checks if a value is in a given list. -
ip() public
Validation of an IP address. -
maxLength() public
Checks whether the length of a string is smaller or equal to a maximal length.. -
minLength() public
Checks whether the length of a string is greater or equal to a minimal length. -
money() public
Checks that a value is a monetary amount. -
multiple() public
Validate a multiple select. -
notEmpty() public
Checks that a string contains something other than whitespace -
numeric() public
Checks if a value is numeric. -
phone() public
Check that a value is a valid phone number. -
postal() public
Checks that a given value is a valid postal code. -
range() public
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
Checks that a value is a valid Social Security Number. -
time() public
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
Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt -
userDefined() public
Runs an user-defined validation. -
uuid() public
Checks that a value is a valid uuid - http://tools.ietf.org/html/rfc4122
Method Detail
_extract() public ¶
_extract( array $params )
Get the values to use when value sent to validation method is an array.
Parameters
- array $params
- Parameters sent to validation method
_ipv4() public ¶
_ipv4( string $check )
Validation of IPv4 addresses.
Parameters
- string $check
- IP Address to test
Returns
Success
_ipv6() public ¶
_ipv6( string $check )
Validation of IPv6 addresses.
Parameters
- string $check
- IP Address to test
Returns
Success
_luhn() public ¶
_luhn( )
Luhn algorithm
Returns
Success
See
_pass() public ¶
_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
Return of Passed method, false on failure
alphaNumeric() public ¶
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
Success
between() public ¶
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
Success
blank() public ¶
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
Success
boolean() public ¶
boolean( string $check )
Boolean validation, determines if value passed is a boolean integer or true/false.
Parameters
- string $check
- a valid boolean
Returns
Success
cc() public ¶
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
Success
See
comparison() public ¶
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
Success
custom() public ¶
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
Success
date() public ¶
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
Success
decimal() public ¶
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
Success
email() public ¶
email( string $check , boolean $deep = false , string $regex = null )
Validates for an email address.
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
Success
equalTo() public ¶
equalTo( mixed $check , mixed $comparedTo )
Check that value is exactly $comparedTo.
Parameters
- mixed $check
- Value to check
- mixed $comparedTo
- Value to compare
Returns
Success
extension() public ¶
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 extenstions to allow
Returns
Success
getInstance() public ¶
getInstance( )
Gets a reference to the Validation object instance
Returns
Validation instance
inList() public ¶
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
Succcess
ip() public ¶
ip( string $check , string $type = 'both' )
Validation of an IP address.
Valid IP version strings for type restriction are: - both: Check both IPv4 and IPv6, return true if the supplied address matches either version - IPv4: Version 4 (Eg: 127.0.0.1, 192.168.10.123, 203.211.24.8) - IPv6: Version 6 (Eg: ::1, 2001:0db8::1428:57ab)
Parameters
- string $check
- The string to test.
- string $type optional 'both'
- The IP Version to test against
Returns
Success
maxLength() public ¶
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
Success
minLength() public ¶
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
Success
money() public ¶
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
Success
multiple() public ¶
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 => maximun 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
Success
notEmpty() public ¶
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
Success
numeric() public ¶
numeric( string $check )
Checks if a value is numeric.
Parameters
- string $check
- Value to check
Returns
Succcess
phone() public ¶
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
Success
postal() public ¶
postal( mixed $check , string $regex = null , string $country = null )
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 null
- Country to use for formatting
Returns
Success
range() public ¶
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
Success
ssn() public ¶
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
Success
time() public ¶
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
Success
url() public ¶
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
Success
userDefined() public ¶
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
user-defined class class method returns
Methods inherited from Object
Object() public ¶
Object( )
A hack to support __construct() on PHP 4 Hint: descendant classes have no PHP4 class_name() constructors, so this constructor gets called first and calls the top-layer __construct() which (if present) should call parent::__construct()
Returns
__openPersistent() public ¶
__openPersistent( string $name , string $type = null )
Open the persistent class file for reading Used by Object::_persist()
Parameters
- string $name
- Name of persisted class
- string $type optional null
- Type of persistance (e.g: registry)
_persist() public ¶
_persist( string $name , string $return , $object , $type = null )
Checks for a persistent class file, if found file is opened and true returned If file is not found a file is created and false returned If used in other locations of the model you should choose a unique name for the persistent file There are many uses for this method, see manual for examples
Parameters
- string $name
- name of the class to persist
- string $return
- $object the object to persist
- $object
- $type optional null
Returns
Success
_savePersistent() public ¶
_savePersistent( string $name , object $object )
You should choose a unique name for the persistent file
There are many uses for this method, see manual for examples
Parameters
- string $name
- name used for object to cache
- object $object
- the object to persist
Returns
true on save, throws error if file can not be created
_set() public ¶
_set( array $properties = array() )
Allows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.
Parameters
- array $properties optional array()
- An associative array containing properties and corresponding values.
_stop() public ¶
_stop( $status = 0 )
Stop execution of the current script. Wraps exit() making testing easier.
Parameters
- $status optional 0
- http://php.net/exit for values
cakeError() public ¶
cakeError( string $method , array $messages = array() )
Used to report user friendly errors. If there is a file app/error.php or app/app_error.php this file will be loaded error.php is the AppError class it should extend ErrorHandler class.
Parameters
- string $method
- Method to be called in the error class (AppError or ErrorHandler classes)
- array $messages optional array()
- Message that is to be displayed by the error class
Returns
message
dispatchMethod() public ¶
dispatchMethod( string $method , array $params = array() )
Calls a method on this object with the given parameters. Provides an OO wrapper
for call_user_func_array
Parameters
- string $method
- Name of the method to call
- array $params optional array()
- Parameter list to use when calling $method
Returns
Returns the result of the method call
log() public ¶
log( string $msg , integer $type = LOG_ERROR )
Convience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
Parameters
- string $msg
- Log message
- integer $type optional LOG_ERROR
- Error type constant. Defined in app/config/core.php.
Returns
Success of log write
requestAction() public ¶
requestAction( mixed $url , array $extra = array() )
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Parameters
- mixed $url
- String or array-based url.
- array $extra optional array()
- if array includes the key "return" it sets the AutoRender to true.
Returns
Boolean true or false on success/failure, or contents of rendered action if 'return' is set in $extra.
toString() public ¶
toString( )
Object-to-string conversion. Each class can override this method as necessary.
Returns
The name of this class
Properties detail
$__pattern ¶
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)' )
$country ¶
Some class methods use a country to determine proper validation. This can be passed to methods in the $country param
null
$errors ¶
Holds an array of errors messages set in this class. These are used for debugging purposes
array()