Modules
Kohana_Validate
extends ArrayObject
Array and variable validation.
Class declared in SYSPATH/classes/kohana/validate.php on line 11.
Constants
Properties
Methods
- __construct()
- alpha()
- alpha_dash()
- alpha_numeric()
- as_array()
- callback()
- callbacks()
- check()
- color()
- copy()
- credit_card()
- date()
- decimal()
- digit()
- email()
- email_domain()
- equals()
- error()
- errors()
- exact_length()
- factory()
- filter()
- filters()
- ip()
- label()
- labels()
- luhn()
- max_length()
- min_length()
- not_empty()
- numeric()
- phone()
- range()
- regex()
- rule()
- rules()
- url()
- append()
- asort()
- count()
- exchangeArray()
- getArrayCopy()
- getFlags()
- getIterator()
- getIteratorClass()
- ksort()
- natcasesort()
- natsort()
- offsetExists()
- offsetGet()
- offsetSet()
- offsetUnset()
- serialize()
- setFlags()
- setIteratorClass()
- uasort()
- uksort()
- unserialize()
- matches()
Constants
STD_PROP_LIST
integer 1
ARRAY_AS_PROPS
integer 2
Properties
protected
$_callbacksprotected
$_empty_rulesprotected
$_errorsprotected
$_filtersprotected
$_labelsprotected
$_rules
Methods
public __construct( array $array ) (defined in Kohana_Validate)
Sets the unique "any field" key and creates an ArrayObject from the passed array.
Parameters
-
array$array required - Array to validate
Return Values
void
Source Code
public function __construct(array $array)
{
parent::__construct($array, ArrayObject::STD_PROP_LIST);
}
public static alpha( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Validate)
Checks whether a string consists of alphabetical characters only.
Parameters
-
string$str required - Input string -
boolean$utf8 = bool FALSE - Trigger UTF-8 compatibility
Return Values
boolean
Source Code
public static function alpha($str, $utf8 = FALSE)
{
$str = (string) $str;
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pL++$/uD', $str);
}
else
{
return ctype_alpha($str);
}
}
public static alpha_dash( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Validate)
Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
Parameters
-
string$str required - Input string -
boolean$utf8 = bool FALSE - Trigger UTF-8 compatibility
Return Values
boolean
Source Code
public static function alpha_dash($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
$regex = '/^[-\pL\pN_]++$/uD';
}
else
{
$regex = '/^[-a-z0-9_]++$/iD';
}
return (bool) preg_match($regex, $str);
}
public static alpha_numeric( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Validate)
Checks whether a string consists of alphabetical characters and numbers only.
Parameters
-
string$str required - Input string -
boolean$utf8 = bool FALSE - Trigger UTF-8 compatibility
Return Values
boolean
Source Code
public static function alpha_numeric($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^[\pL\pN]++$/uD', $str);
}
else
{
return ctype_alnum($str);
}
}
public as_array( ) (defined in Kohana_Validate)
Returns the array representation of the current object.
Return Values
array
Source Code
public function as_array()
{
return $this->getArrayCopy();
}
public callback( string $field , mixed $callback [, array $params = array(0) ] ) (defined in Kohana_Validate)
Adds a callback to a field. Each callback will be executed only once.
// The "username" must be checked with a custom method
$validation->callback('username', array($this, 'check_username'));
To add a callback to every field already set, use TRUE for the field name.
Parameters
-
string$field required - Field name -
mixed$callback required - Callback to add -
array$params = array(0) - Extra parameters for the callback
Return Values
$this
Source Code
public function callback($field, $callback, array $params = array())
{
if ( ! isset($this->_callbacks[$field]))
{
// Create the list for this field
$this->_callbacks[$field] = array();
}
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
}
if ( ! in_array($callback, $this->_callbacks[$field], TRUE))
{
// Store the callback
$this->_callbacks[$field][] = array($callback, $params);
}
return $this;
}
public callbacks( string $field , array $callbacks ) (defined in Kohana_Validate)
Add callbacks using an array.
Parameters
-
string$field required - Field name -
array$callbacks required - List of callbacks
Return Values
$this
Source Code
public function callbacks($field, array $callbacks)
{
foreach ($callbacks as $callback)
{
$this->callback($field, $callback);
}
return $this;
}
public check( [ boolean $allow_empty = bool FALSE ] ) (defined in Kohana_Validate)
Executes all validation filters, rules, and callbacks. This should typically be called within an if/else block.
if ($validation->check())
{
// The data is valid, do something here
}
Parameters
-
boolean$allow_empty = bool FALSE - Allow empty array?
Return Values
boolean
Source Code
public function check($allow_empty = FALSE)
{
if (Kohana::$profiling === TRUE)
{
// Start a new benchmark
$benchmark = Profiler::start('Validation', __FUNCTION__);
}
// New data set
$data = $this->_errors = array();
// Assume nothing has been submitted
$submitted = FALSE;
// Get a list of the expected fields
$expected = array_keys($this->_labels);
// Import the filters, rules, and callbacks locally
$filters = $this->_filters;
$rules = $this->_rules;
$callbacks = $this->_callbacks;
foreach ($expected as $field)
{
if (isset($this[$field]))
{
// Some data has been submitted, continue validation
$submitted = TRUE;
// Use the submitted value
$data[$field] = $this[$field];
}
else
{
// No data exists for this field
$data[$field] = NULL;
}
if (isset($filters[TRUE]))
{
if ( ! isset($filters[$field]))
{
// Initialize the filters for this field
$filters[$field] = array();
}
// Append the filters
$filters[$field] = array_merge($filters[$field], $filters[TRUE]);
}
if (isset($rules[TRUE]))
{
if ( ! isset($rules[$field]))
{
// Initialize the rules for this field
$rules[$field] = array();
}
// Append the rules
$rules[$field] = array_merge($rules[$field], $rules[TRUE]);
}
if (isset($callbacks[TRUE]))
{
if ( ! isset($callbacks[$field]))
{
// Initialize the callbacks for this field
$callbacks[$field] = array();
}
// Append the callbacks
$callbacks[$field] = array_merge($callbacks[$field], $callbacks[TRUE]);
}
}
// Overload the current array with the new one
$this->exchangeArray($data);
if ($submitted === FALSE AND ! $allow_empty)
{
// Because no data was submitted, validation will not be forced
return FALSE;
}
// Remove the filters, rules, and callbacks that apply to every field
unset($filters[TRUE], $rules[TRUE], $callbacks[TRUE]);
// Execute the filters
foreach ($filters as $field => $set)
{
// Get the field value
$value = $this[$field];
foreach ($set as $filter => $params)
{
// Add the field value to the parameters
array_unshift($params, $value);
if (strpos($filter, '::') === FALSE)
{
// Use a function call
$function = new ReflectionFunction($filter);
// Call $function($this[$field], $param, ...) with Reflection
$value = $function->invokeArgs($params);
}
else
{
// Split the class and method of the rule
list($class, $method) = explode('::', $filter, 2);
// Use a static method call
$method = new ReflectionMethod($class, $method);
// Call $Class::$method($this[$field], $param, ...) with Reflection
$value = $method->invokeArgs(NULL, $params);
}
}
// Set the filtered value
$this[$field] = $value;
}
// Execute the rules
foreach ($rules as $field => $set)
{
// Get the field value
$value = $this[$field];
foreach ($set as $rule => $params)
{
if ( ! in_array($rule, $this->_empty_rules) AND ! Validate::not_empty($value))
{
// Skip this rule for empty fields
continue;
}
// Add the field value to the parameters
array_unshift($params, $value);
if (method_exists($this, $rule))
{
// Use a method in this object
$method = new ReflectionMethod($this, $rule);
if ($method->isStatic())
{
// Call static::$rule($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
else
{
// Do not use Reflection here, the method may be protected
$passed = call_user_func_array(array($this, $rule), $params);
}
}
elseif (strpos($rule, '::') === FALSE)
{
// Use a function call
$function = new ReflectionFunction($rule);
// Call $function($this[$field], $param, ...) with Reflection
$passed = $function->invokeArgs($params);
}
else
{
// Split the class and method of the rule
list($class, $method) = explode('::', $rule, 2);
// Use a static method call
$method = new ReflectionMethod($class, $method);
// Call $Class::$method($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
if ($passed === FALSE)
{
// Remove the field value from the parameters
array_shift($params);
// Add the rule to the errors
$this->error($field, $rule, $params);
// This field has an error, stop executing rules
break;
}
}
}
// Execute the callbacks
foreach ($callbacks as $field => $set)
{
if (isset($this->_errors[$field]))
{
// Skip any field that already has an error
continue;
}
foreach ($set as $callback_array)
{
list($callback, $params) = $callback_array;
if (is_string($callback) AND strpos($callback, '::') !== FALSE)
{
// Make the static callback into an array
$callback = explode('::', $callback, 2);
}
if (is_array($callback))
{
// Separate the object and method
list ($object, $method) = $callback;
// Use a method in the given object
$method = new ReflectionMethod($object, $method);
if ( ! is_object($object))
{
// The object must be NULL for static calls
$object = NULL;
}
// Call $object->$method($this, $field, $errors) with Reflection
$method->invoke($object, $this, $field, $params);
}
else
{
// Use a function call
$function = new ReflectionFunction($callback);
// Call $function($this, $field, $errors) with Reflection
$function->invoke($this, $field, $params);
}
if (isset($this->_errors[$field]))
{
// An error was added, stop processing callbacks
break;
}
}
}
if (isset($benchmark))
{
// Stop benchmarking
Profiler::stop($benchmark);
}
return empty($this->_errors);
}
public static color( string $str ) (defined in Kohana_Validate)
Checks if a string is a proper hexadecimal HTML color value. The validation is quite flexible as it does not require an initial "#" and also allows for the short notation using only three instead of six hexadecimal characters.
Parameters
-
string$str required - Input string
Return Values
boolean
Source Code
public static function color($str)
{
return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $str);
}
public copy( array $array ) (defined in Kohana_Validate)
Copies the current filter/rule/callback to a new array.
$copy = $array->copy($new_data);
Parameters
-
array$array required - New data set
Tags
Return Values
Validation
Source Code
public function copy(array $array)
{
// Create a copy of the current validation set
$copy = clone $this;
// Replace the data set
$copy->exchangeArray($array);
return $copy;
}
public static credit_card( integer $number [, string|array $type = NULL ] ) (defined in Kohana_Validate)
Validates a credit card number, with a Luhn check if possible.
Parameters
-
integer$number required - Credit card number -
string|array$type = NULL - Card type, or an array of card types
Tags
Return Values
boolean
Source Code
public static function credit_card($number, $type = NULL)
{
// Remove all non-digit characters from the number
if (($number = preg_replace('/\D+/', '', $number)) === '')
return FALSE;
if ($type == NULL)
{
// Use the default type
$type = 'default';
}
elseif (is_array($type))
{
foreach ($type as $t)
{
// Test each type for validity
if (Validate::credit_card($number, $t))
return TRUE;
}
return FALSE;
}
$cards = Kohana::config('credit_cards');
// Check card type
$type = strtolower($type);
if ( ! isset($cards[$type]))
return FALSE;
// Check card number length
$length = strlen($number);
// Validate the card length by the card type
if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
return FALSE;
// Check card number prefix
if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
return FALSE;
// No Luhn check required
if ($cards[$type]['luhn'] == FALSE)
return TRUE;
return Validate::luhn($number);
}
public static date( string $str ) (defined in Kohana_Validate)
Tests if a string is a valid date string.
Parameters
-
string$str required - Date to check
Return Values
boolean
Source Code
public static function date($str)
{
return (strtotime($str) !== FALSE);
}
public static decimal( string $str [, integer $places = integer 2 , integer $digits = NULL ] ) (defined in Kohana_Validate)
Checks if a string is a proper decimal format. Optionally, a specific number of digits can be checked too.
Parameters
-
string$str required - Number to check -
integer$places = integer 2 - Number of decimal places -
integer$digits = NULL - Number of digits
Return Values
boolean
Source Code
public static function decimal($str, $places = 2, $digits = NULL)
{
if ($digits > 0)
{
// Specific number of digits
$digits = '{'. (int) $digits.'}';
}
else
{
// Any number of digits
$digits = '+';
}
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
return (bool) preg_match('/^[0-9]'.$digits.preg_quote($decimal).'[0-9]{'. (int) $places.'}$/D', $str);
}
public static digit( string $str [, boolean $utf8 = bool FALSE ] ) (defined in Kohana_Validate)
Checks whether a string consists of digits only (no dots or dashes).
Parameters
-
string$str required - Input string -
boolean$utf8 = bool FALSE - Trigger UTF-8 compatibility
Return Values
boolean
Source Code
public static function digit($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pN++$/uD', $str);
}
else
{
return (is_int($str) AND $str >= 0) OR ctype_digit($str);
}
}
public static email( string $email [, boolean $strict = bool FALSE ] ) (defined in Kohana_Validate)
Check an email address for correct format.
Parameters
-
string$email required - Email address -
boolean$strict = bool FALSE - Strict RFC compatibility
Tags
Return Values
boolean
Source Code
public static function email($email, $strict = FALSE)
{
if ($strict === TRUE)
{
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$pair)*\\x5d";
$quoted_string = "\\x22($qtext|$pair)*\\x22";
$sub_domain = "($atom|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$expression = "/^$local_part\\x40$domain$/D";
}
else
{
$expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD';
}
return (bool) preg_match($expression, (string) $email);
}
public static email_domain( string $email ) (defined in Kohana_Validate)
Validate the domain of an email address by checking if the domain has a valid MX record.
Parameters
-
string$email required - Email address
Tags
Return Values
boolean
Source Code
public static function email_domain($email)
{
// Check if the email domain has a valid MX record
return (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
}
public static equals( string $value , string $required ) (defined in Kohana_Validate)
Checks that a field is exactly the value required.
Parameters
-
string$value required - Value -
string$required required - Required value
Return Values
boolean
Source Code
public static function equals($value, $required)
{
return ($value === $required);
}
public error( string $field , string $error [, $params = NULL ] ) (defined in Kohana_Validate)
Add an error to a field.
Parameters
-
string$field required - Field name -
string$error required - Error message -
unknown$params = NULL
Return Values
$this
Source Code
public function error($field, $error, array $params = NULL)
{
$this->_errors[$field] = array($error, $params);
return $this;
}
public errors( [ string $file = NULL , mixed $translate = bool TRUE ] ) (defined in Kohana_Validate)
Returns the error messages. If no file is specified, the error message
will be the name of the rule that failed. When a file is specified, the
message will be loaded from $field.$rule, or if no rule-specific message
exists, $field.default will be used. If neither is set, the returned
message will be validate.$rule. If validate.$rule is empty,
then $file.$field.$rule will be returned.
By default all messages are translated using the default language. A string can be used as the second parameter to specified the language that the message was written in.
// Get errors from messages/forms/login.php
$errors = $validate->errors('forms/login');
Parameters
-
string$file = NULL - File to load error messages from -
mixed$translate = bool TRUE - Translate the message
Tags
Return Values
array
Source Code
public function errors($file = NULL, $translate = TRUE)
{
if ($file === NULL)
{
// Return the error list
return $this->_errors;
}
// Create a new message list
$messages = array();
foreach ($this->_errors as $field => $set)
{
list($error, $params) = $set;
// Get the label for this field
$label = $this->_labels[$field];
if ($translate)
{
if (is_string($translate))
{
// Translate the label using the specified language
$label = __($label, NULL, $translate);
}
else
{
// Translate the label
$label = __($label);
}
}
// Start the translation values list
$values = array(
':field' => $label,
':value' => $this[$field],
);
if (is_array($values[':value']))
{
// All values must be strings
$values[':value'] = implode(', ', Arr::flatten($values[':value']));
}
if ($params)
{
foreach ($params as $key => $value)
{
if (is_array($value))
{
// All values must be strings
$value = implode(', ', Arr::flatten($value));
}
// Check if a label for this parameter exists
if (isset($this->_labels[$value]))
{
// Use the label as the value, eg: related field name for "matches"
$value = $this->_labels[$value];
if ($translate)
{
if (is_string($translate))
{
// Translate the value using the specified language
$value = __($value, NULL, $translate);
}
else
{
// Translate the value
$value = __($value);
}
}
}
// Add each parameter as a numbered value, starting from 1
$values[':param'.($key + 1)] = $value;
}
}
if ($message = Kohana::message($file, "{$field}.{$error}") AND is_string($message))
{
// Found a message for this field and error
}
elseif ($message = Kohana::message($file, "{$field}.default") AND is_string($message))
{
// Found a default message for this field
}
elseif ($message = Kohana::message($file, $error) AND is_string($message))
{
// Found a default message for this error
}
elseif ($message = Kohana::message('validate', $error))
{
// Found a default message for this error
}
else
{
// No message exists, display the path expected
$message = "{$file}.{$field}.{$error}";
}
if ($translate)
{
if (is_string($translate))
{
// Translate the message using specified language
$message = __($message, $values, $translate);
}
else
{
// Translate the message using the default language
$message = __($message, $values);
}
}
else
{
// Do not translate, just replace the values
$message = strtr($message, $values);
}
// Set the message for this field
$messages[$field] = $message;
}
return $messages;
}
public static exact_length( string $value , integer $length ) (defined in Kohana_Validate)
Checks that a field is exactly the right length.
Parameters
-
string$value required - Value -
integer$length required - Exact length required
Return Values
boolean
Source Code
public static function exact_length($value, $length)
{
return UTF8::strlen($value) === $length;
}
public static factory( array $array ) (defined in Kohana_Validate)
Creates a new Validation instance.
Parameters
-
array$array required - Array to use for validation
Return Values
Validate
Source Code
public static function factory(array $array)
{
return new Validate($array);
}
public filter( string $field , mixed $filter [, array $params = NULL ] ) (defined in Kohana_Validate)
Overwrites or appends filters to a field. Each filter will be executed once. All rules must be valid PHP callbacks.
// Run trim() on all fields
$validation->filter(TRUE, 'trim');
Parameters
-
string$field required - Field name -
mixed$filter required - Valid PHP callback -
array$params = NULL - Extra parameters for the filter
Return Values
$this
Source Code
public function filter($field, $filter, array $params = NULL)
{
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
}
// Store the filter and params for this rule
$this->_filters[$field][$filter] = (array) $params;
return $this;
}
public filters( string $field , array $filters ) (defined in Kohana_Validate)
Add filters using an array.
Parameters
-
string$field required - Field name -
array$filters required - List of functions or static method name
Return Values
$this
Source Code
public function filters($field, array $filters)
{
foreach ($filters as $filter => $params)
{
$this->filter($field, $filter, $params);
}
return $this;
}
public static ip( string $ip [, boolean $allow_private = bool TRUE ] ) (defined in Kohana_Validate)
Validate an IP.
Parameters
-
string$ip required - IP address -
boolean$allow_private = bool TRUE - Allow private IP networks
Return Values
boolean
Source Code
public static function ip($ip, $allow_private = TRUE)
{
// Do not allow reserved addresses
$flags = FILTER_FLAG_NO_RES_RANGE;
if ($allow_private === FALSE)
{
// Do not allow private or reserved addresses
$flags = $flags | FILTER_FLAG_NO_PRIV_RANGE;
}
return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
}
public label( string $field , string $label ) (defined in Kohana_Validate)
Sets or overwrites the label name for a field.
Parameters
-
string$field required - Field name -
string$label required - Label
Return Values
$this
Source Code
public function label($field, $label)
{
// Set the label for this field
$this->_labels[$field] = $label;
return $this;
}
public labels( array $labels ) (defined in Kohana_Validate)
Sets labels using an array.
Parameters
-
array$labels required - List of field => label names
Return Values
$this
Source Code
public function labels(array $labels)
{
$this->_labels = $labels + $this->_labels;
return $this;
}
public static luhn( string $number ) (defined in Kohana_Validate)
Validate a number against the Luhn (mod10) formula.
Parameters
-
string$number required - Number to check
Return Values
boolean
Source Code
public static function luhn($number)
{
// Force the value to be a string as this method uses string functions.
// Converting to an integer may pass PHP_INT_MAX and result in an error!
$number = (string) $number;
if ( ! ctype_digit($number))
{
// Luhn can only be used on numbers!
return FALSE;
}
// Check number length
$length = strlen($number);
// Checksum of the card number
$checksum = 0;
for ($i = $length - 1; $i >= 0; $i -= 2)
{
// Add up every 2nd digit, starting from the right
$checksum += substr($number, $i, 1);
}
for ($i = $length - 2; $i >= 0; $i -= 2)
{
// Add up every 2nd digit doubled, starting from the right
$double = substr($number, $i, 1) * 2;
// Subtract 9 from the double where value is greater than 10
$checksum += ($double >= 10) ? ($double - 9) : $double;
}
// If the checksum is a multiple of 10, the number is valid
return ($checksum % 10 === 0);
}
public static max_length( string $value , integer $length ) (defined in Kohana_Validate)
Checks that a field is short enough.
Parameters
-
string$value required - Value -
integer$length required - Maximum length required
Return Values
boolean
Source Code
public static function max_length($value, $length)
{
return UTF8::strlen($value) <= $length;
}
public static min_length( string $value , integer $length ) (defined in Kohana_Validate)
Checks that a field is long enough.
Parameters
-
string$value required - Value -
integer$length required - Minimum length required
Return Values
boolean
Source Code
public static function min_length($value, $length)
{
return UTF8::strlen($value) >= $length;
}
public static not_empty( ) (defined in Kohana_Validate)
Checks if a field is not empty.
Return Values
boolean
Source Code
public static function not_empty($value)
{
if (is_object($value) AND $value instanceof ArrayObject)
{
// Get the array from the ArrayObject
$value = $value->getArrayCopy();
}
// Value cannot be NULL, FALSE, '', or an empty array
return ! in_array($value, array(NULL, FALSE, '', array()), TRUE);
}
public static numeric( string $str ) (defined in Kohana_Validate)
Checks whether a string is a valid number (negative and decimal numbers allowed).
Uses {@link http://www.php.net/manual/en/function.localeconv.php locale conversion} to allow decimal point to be locale specific.
Parameters
-
string$str required - Input string
Return Values
boolean
Source Code
public static function numeric($str)
{
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
// A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)
return (bool) preg_match('/^-?+(?=.*[0-9])[0-9]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str);
}
public static phone( string $number [, $lengths = NULL ] ) (defined in Kohana_Validate)
Checks if a phone number is valid.
Parameters
-
string$number required - Phone number to check -
unknown$lengths = NULL
Return Values
boolean
Source Code
public static function phone($number, $lengths = NULL)
{
if ( ! is_array($lengths))
{
$lengths = array(7,10,11);
}
// Remove all non-digit characters from the number
$number = preg_replace('/\D+/', '', $number);
// Check if the number is within range
return in_array(strlen($number), $lengths);
}
public static range( string $number , integer $min , integer $max ) (defined in Kohana_Validate)
Tests if a number is within a range.
Parameters
-
string$number required - Number to check -
integer$min required - Minimum value -
integer$max required - Maximum value
Return Values
boolean
Source Code
public static function range($number, $min, $max)
{
return ($number >= $min AND $number <= $max);
}
public static regex( string $value , string $expression ) (defined in Kohana_Validate)
Checks a field against a regular expression.
Parameters
-
string$value required - Value -
string$expression required - Regular expression to match (including delimiters)
Return Values
boolean
Source Code
public static function regex($value, $expression)
{
return (bool) preg_match($expression, (string) $value);
}
public rule( string $field , string $rule [, array $params = NULL ] ) (defined in Kohana_Validate)
Overwrites or appends rules to a field. Each rule will be executed once. All rules must be string names of functions method names.
// The "username" must not be empty and have a minimum length of 4
$validation->rule('username', 'not_empty')
->rule('username', 'min_length', array(4));
Parameters
-
string$field required - Field name -
string$rule required - Function or static method name -
array$params = NULL - Extra parameters for the rule
Return Values
$this
Source Code
public function rule($field, $rule, array $params = NULL)
{
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = trim(preg_replace('/[^\pL]+/u', ' ', $field));
}
if ('matches' === $rule AND ! isset($this->_labels[$params[0]]))
{
$match_field = $params[0];
$this->_labels[$match_field] = trim(preg_replace('/[^\pL]+/u', ' ', $match_field));
}
// Store the rule and params for this rule
$this->_rules[$field][$rule] = (array) $params;
return $this;
}
public rules( string $field , array $rules ) (defined in Kohana_Validate)
Add rules using an array.
Parameters
-
string$field required - Field name -
array$rules required - List of functions or static method name
Return Values
$this
Source Code
public function rules($field, array $rules)
{
foreach ($rules as $rule => $params)
{
$this->rule($field, $rule, $params);
}
return $this;
}
public static url( string $url ) (defined in Kohana_Validate)
Validate a URL.
Parameters
-
string$url required - URL
Return Values
boolean
Source Code
public static function url($url)
{
// Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
if ( ! preg_match(
'~^
# scheme
[-a-z0-9+.]++://
# username:password (optional)
(?:
[-a-z0-9$_.+!*\'(),;?&=%]++ # username
(?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
@
)?
(?:
# ip address
\d{1,3}+(?:\.\d{1,3}+){3}+
| # or
# hostname (captured)
(
(?!-)[-a-z0-9]{1,63}+(?<!-)
(?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
)
)
# port (optional)
(?::\d{1,5}+)?
# path (optional)
(?:/.*)?
$~iDx', $url, $matches))
return FALSE;
// We matched an IP address
if ( ! isset($matches[1]))
return TRUE;
// Check maximum length of the whole hostname
// http://en.wikipedia.org/wiki/Domain_name#cite_note-0
if (strlen($matches[1]) > 253)
return FALSE;
// An extra check for the top level domain
// It must start with a letter
$tld = ltrim(substr($matches[1], (int) strrpos($matches[1], '.')), '.');
return ctype_alpha($tld[0]);
}
public append( ) (defined in ArrayObject)
public asort( ) (defined in ArrayObject)
public count( ) (defined in ArrayObject)
public exchangeArray( ) (defined in ArrayObject)
public getArrayCopy( ) (defined in ArrayObject)
public getFlags( ) (defined in ArrayObject)
public getIterator( ) (defined in ArrayObject)
public getIteratorClass( ) (defined in ArrayObject)
public ksort( ) (defined in ArrayObject)
public natcasesort( ) (defined in ArrayObject)
public natsort( ) (defined in ArrayObject)
public offsetExists( ) (defined in ArrayObject)
public offsetGet( ) (defined in ArrayObject)
public offsetSet( ) (defined in ArrayObject)
public offsetUnset( ) (defined in ArrayObject)
public serialize( ) (defined in ArrayObject)
public setFlags( ) (defined in ArrayObject)
public setIteratorClass( ) (defined in ArrayObject)
public uasort( ) (defined in ArrayObject)
public uksort( ) (defined in ArrayObject)
public unserialize( ) (defined in ArrayObject)
protected matches( string $value , string $match ) (defined in Kohana_Validate)
Checks if a field matches the value of another field.
Parameters
-
string$value required - Field value -
string$match required - Field name to match
Return Values
boolean
Source Code
protected function matches($value, $match)
{
return ($value === $this[$match]);
}