mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #263 from vlucas/iss262
Filter out non-required array parts.
This commit is contained in:
commit
b6e9079d16
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
|||||||
composer.phar
|
composer.phar
|
||||||
composer.lock
|
composer.lock
|
||||||
vendor
|
vendor
|
||||||
|
.idea/
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Valitron;
|
namespace Valitron;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,10 +86,10 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Setup validation
|
* Setup validation
|
||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @param array $fields
|
* @param array $fields
|
||||||
* @param string $lang
|
* @param string $lang
|
||||||
* @param string $langDir
|
* @param string $langDir
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function __construct($data = array(), $fields = array(), $lang = null, $langDir = null)
|
public function __construct($data = array(), $fields = array(), $lang = null, $langDir = null)
|
||||||
@ -105,7 +106,7 @@ class Validator
|
|||||||
|
|
||||||
// Load language file in directory
|
// Load language file in directory
|
||||||
$langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
|
$langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
|
||||||
if (stream_resolve_include_path($langFile) ) {
|
if (stream_resolve_include_path($langFile)) {
|
||||||
$langMessages = include $langFile;
|
$langMessages = include $langFile;
|
||||||
static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
|
static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
|
||||||
} else {
|
} else {
|
||||||
@ -147,13 +148,13 @@ class Validator
|
|||||||
* Required field validator
|
* Required field validator
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateRequired($field, $value, $params= array())
|
protected function validateRequired($field, $value, $params = array())
|
||||||
{
|
{
|
||||||
if (isset($params[0]) && (bool) $params[0]){
|
if (isset($params[0]) && (bool)$params[0]) {
|
||||||
$find = $this->getPart($this->_fields, explode('.', $field), true);
|
$find = $this->getPart($this->_fields, explode('.', $field), true);
|
||||||
return $find[1];
|
return $find[1];
|
||||||
}
|
}
|
||||||
@ -171,8 +172,8 @@ class Validator
|
|||||||
* Validate that two values match
|
* Validate that two values match
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -187,8 +188,8 @@ class Validator
|
|||||||
* Validate that a field is different from another field
|
* Validate that a field is different from another field
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -205,7 +206,7 @@ class Validator
|
|||||||
* This validation rule implies the field is "required"
|
* This validation rule implies the field is "required"
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateAccepted($field, $value)
|
protected function validateAccepted($field, $value)
|
||||||
@ -219,7 +220,7 @@ class Validator
|
|||||||
* Validate that a field is an array
|
* Validate that a field is an array
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateArray($field, $value)
|
protected function validateArray($field, $value)
|
||||||
@ -231,7 +232,7 @@ class Validator
|
|||||||
* Validate that a field is numeric
|
* Validate that a field is numeric
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateNumeric($field, $value)
|
protected function validateNumeric($field, $value)
|
||||||
@ -243,13 +244,13 @@ class Validator
|
|||||||
* Validate that a field is an integer
|
* Validate that a field is an integer
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateInteger($field, $value, $params)
|
protected function validateInteger($field, $value, $params)
|
||||||
{
|
{
|
||||||
if (isset($params[0]) && (bool) $params[0]){
|
if (isset($params[0]) && (bool)$params[0]) {
|
||||||
//strict mode
|
//strict mode
|
||||||
return preg_match('/^([0-9]|-[1-9]|-?[1-9][0-9]*)$/i', $value);
|
return preg_match('/^([0-9]|-[1-9]|-?[1-9][0-9]*)$/i', $value);
|
||||||
}
|
}
|
||||||
@ -261,8 +262,8 @@ class Validator
|
|||||||
* Validate the length of a string
|
* Validate the length of a string
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -280,9 +281,9 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Validate the length of a string (between)
|
* Validate the length of a string (between)
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
protected function validateLengthBetween($field, $value, $params)
|
protected function validateLengthBetween($field, $value, $params)
|
||||||
@ -296,8 +297,8 @@ class Validator
|
|||||||
* Validate the length of a string (min)
|
* Validate the length of a string (min)
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
@ -312,8 +313,8 @@ class Validator
|
|||||||
* Validate the length of a string (max)
|
* Validate the length of a string (max)
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
@ -345,8 +346,8 @@ class Validator
|
|||||||
* Validate the size of a field is greater than a minimum value.
|
* Validate the size of a field is greater than a minimum value.
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -365,8 +366,8 @@ class Validator
|
|||||||
* Validate the size of a field is less than a maximum value
|
* Validate the size of a field is less than a maximum value
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -385,9 +386,8 @@ class Validator
|
|||||||
* Validate the size of a field is between min and max values
|
* Validate the size of a field is between min and max values
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateBetween($field, $value, $params)
|
protected function validateBetween($field, $value, $params)
|
||||||
@ -408,8 +408,8 @@ class Validator
|
|||||||
* Validate a field is contained within a list of values
|
* Validate a field is contained within a list of values
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -432,8 +432,8 @@ class Validator
|
|||||||
* Validate a field is not contained within a list of values
|
* Validate a field is not contained within a list of values
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -446,8 +446,8 @@ class Validator
|
|||||||
* Validate a field contains a given string
|
* Validate a field contains a given string
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateContains($field, $value, $params)
|
protected function validateContains($field, $value, $params)
|
||||||
@ -461,7 +461,7 @@ class Validator
|
|||||||
|
|
||||||
$strict = true;
|
$strict = true;
|
||||||
if (isset($params[1])) {
|
if (isset($params[1])) {
|
||||||
$strict = (bool) $params[1];
|
$strict = (bool)$params[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
$isContains = false;
|
$isContains = false;
|
||||||
@ -485,7 +485,7 @@ class Validator
|
|||||||
* Validate that a field is a valid IP address
|
* Validate that a field is a valid IP address
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateIp($field, $value)
|
protected function validateIp($field, $value)
|
||||||
@ -497,7 +497,7 @@ class Validator
|
|||||||
* Validate that a field is a valid e-mail address
|
* Validate that a field is a valid e-mail address
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateEmail($field, $value)
|
protected function validateEmail($field, $value)
|
||||||
@ -509,7 +509,7 @@ class Validator
|
|||||||
* Validate that a field is a valid e-mail address and the domain name is active
|
* Validate that a field is a valid e-mail address and the domain name is active
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateEmailDNS($field, $value)
|
protected function validateEmailDNS($field, $value)
|
||||||
@ -529,7 +529,7 @@ class Validator
|
|||||||
* Validate that a field is a valid URL by syntax
|
* Validate that a field is a valid URL by syntax
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateUrl($field, $value)
|
protected function validateUrl($field, $value)
|
||||||
@ -547,7 +547,7 @@ class Validator
|
|||||||
* Validate that a field is an active URL by verifying DNS record
|
* Validate that a field is an active URL by verifying DNS record
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateUrlActive($field, $value)
|
protected function validateUrlActive($field, $value)
|
||||||
@ -567,7 +567,7 @@ class Validator
|
|||||||
* Validate that a field contains only alphabetic characters
|
* Validate that a field contains only alphabetic characters
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateAlpha($field, $value)
|
protected function validateAlpha($field, $value)
|
||||||
@ -579,7 +579,7 @@ class Validator
|
|||||||
* Validate that a field contains only alpha-numeric characters
|
* Validate that a field contains only alpha-numeric characters
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateAlphaNum($field, $value)
|
protected function validateAlphaNum($field, $value)
|
||||||
@ -591,12 +591,12 @@ class Validator
|
|||||||
* Validate that a field contains only alpha-numeric characters, dashes, and underscores
|
* Validate that a field contains only alpha-numeric characters, dashes, and underscores
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateSlug($field, $value)
|
protected function validateSlug($field, $value)
|
||||||
{
|
{
|
||||||
if(is_array($value)) {
|
if (is_array($value)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return preg_match('/^([-a-z0-9_-])+$/i', $value);
|
return preg_match('/^([-a-z0-9_-])+$/i', $value);
|
||||||
@ -606,8 +606,8 @@ class Validator
|
|||||||
* Validate that a field passes a regular expression check
|
* Validate that a field passes a regular expression check
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateRegex($field, $value, $params)
|
protected function validateRegex($field, $value, $params)
|
||||||
@ -619,7 +619,7 @@ class Validator
|
|||||||
* Validate that a field is a valid date
|
* Validate that a field is a valid date
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateDate($field, $value)
|
protected function validateDate($field, $value)
|
||||||
@ -638,8 +638,8 @@ class Validator
|
|||||||
* Validate that a field matches a date format
|
* Validate that a field matches a date format
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -654,8 +654,8 @@ class Validator
|
|||||||
* Validate the date is before a given date
|
* Validate the date is before a given date
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -671,8 +671,8 @@ class Validator
|
|||||||
* Validate the date is after a given date
|
* Validate the date is after a given date
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
* @internal param array $fields
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -688,7 +688,7 @@ class Validator
|
|||||||
* Validate that a field contains a boolean.
|
* Validate that a field contains a boolean.
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateBoolean($field, $value)
|
protected function validateBoolean($field, $value)
|
||||||
@ -701,8 +701,8 @@ class Validator
|
|||||||
* optionally filtered by an array
|
* optionally filtered by an array
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateCreditCard($field, $value, $params)
|
protected function validateCreditCard($field, $value, $params)
|
||||||
@ -718,7 +718,7 @@ class Validator
|
|||||||
if (is_array($params[0])) {
|
if (is_array($params[0])) {
|
||||||
$cards = $params[0];
|
$cards = $params[0];
|
||||||
} elseif (is_string($params[0])) {
|
} elseif (is_string($params[0])) {
|
||||||
$cardType = $params[0];
|
$cardType = $params[0];
|
||||||
if (isset($params[1]) && is_array($params[1])) {
|
if (isset($params[1]) && is_array($params[1])) {
|
||||||
$cards = $params[1];
|
$cards = $params[1];
|
||||||
if (!in_array($cardType, $cards)) {
|
if (!in_array($cardType, $cards)) {
|
||||||
@ -741,7 +741,7 @@ class Validator
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for ($i = 0; $i < $strlen; $i++) {
|
for ($i = 0; $i < $strlen; $i++) {
|
||||||
$digit = (int) substr($number, $strlen - $i - 1, 1);
|
$digit = (int)substr($number, $strlen - $i - 1, 1);
|
||||||
if ($i % 2 == 1) {
|
if ($i % 2 == 1) {
|
||||||
$sub_total = $digit * 2;
|
$sub_total = $digit * 2;
|
||||||
if ($sub_total > 9) {
|
if ($sub_total > 9) {
|
||||||
@ -753,7 +753,7 @@ class Validator
|
|||||||
$sum += $sub_total;
|
$sum += $sub_total;
|
||||||
}
|
}
|
||||||
if ($sum > 0 && $sum % 10 == 0) {
|
if ($sum > 0 && $sum % 10 == 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -764,11 +764,11 @@ class Validator
|
|||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
$cardRegex = array(
|
$cardRegex = array(
|
||||||
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
|
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
|
||||||
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
|
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
|
||||||
'amex' => '#^3[47][0-9]{13}$#',
|
'amex' => '#^3[47][0-9]{13}$#',
|
||||||
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
|
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
|
||||||
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
|
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isset($cardType)) {
|
if (isset($cardType)) {
|
||||||
@ -827,7 +827,8 @@ class Validator
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Validate optional field
|
//Validate optional field
|
||||||
protected function validateOptional($field, $value, $params) {
|
protected function validateOptional($field, $value, $params)
|
||||||
|
{
|
||||||
//Always return true
|
//Always return true
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -862,7 +863,7 @@ class Validator
|
|||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param string $msg
|
* @param string $msg
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*/
|
*/
|
||||||
public function error($field, $msg, array $params = array())
|
public function error($field, $msg, array $params = array())
|
||||||
{
|
{
|
||||||
@ -940,24 +941,21 @@ class Validator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return array($values, true);
|
return array($values, true);
|
||||||
}
|
} // Dead end, abort
|
||||||
// Dead end, abort
|
elseif ($identifier === NULL || !isset($data[$identifier])) {
|
||||||
elseif ($identifier === NULL || ! isset($data[$identifier])) {
|
if ($allow_empty) {
|
||||||
if ($allow_empty){
|
|
||||||
//when empty values are allowed, we only care if the key exists
|
//when empty values are allowed, we only care if the key exists
|
||||||
return array(null, array_key_exists($identifier, $data));
|
return array(null, array_key_exists($identifier, $data));
|
||||||
}
|
}
|
||||||
return array(null, false);
|
return array(null, false);
|
||||||
}
|
} // Match array element
|
||||||
// Match array element
|
|
||||||
elseif (count($identifiers) === 0) {
|
elseif (count($identifiers) === 0) {
|
||||||
if ($allow_empty){
|
if ($allow_empty) {
|
||||||
//when empty values are allowed, we only care if the key exists
|
//when empty values are allowed, we only care if the key exists
|
||||||
return array(null, array_key_exists($identifier, $data));
|
return array(null, array_key_exists($identifier, $data));
|
||||||
}
|
}
|
||||||
return array($data[$identifier], false);
|
return array($data[$identifier], $allow_empty);
|
||||||
}
|
} // We need to go deeper
|
||||||
// We need to go deeper
|
|
||||||
else {
|
else {
|
||||||
return $this->getPart($data[$identifier], $identifiers, $allow_empty);
|
return $this->getPart($data[$identifier], $identifiers, $allow_empty);
|
||||||
}
|
}
|
||||||
@ -970,10 +968,10 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
public function validate()
|
public function validate()
|
||||||
{
|
{
|
||||||
$set_to_break = false;
|
$set_to_break = false;
|
||||||
foreach ($this->_validations as $v) {
|
foreach ($this->_validations as $v) {
|
||||||
foreach ($v['fields'] as $field) {
|
foreach ($v['fields'] as $field) {
|
||||||
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field));
|
list($values, $multiple) = $this->getPart($this->_fields, explode('.', $field), false);
|
||||||
|
|
||||||
// Don't validate if the field is not required and the value is empty
|
// Don't validate if the field is not required and the value is empty
|
||||||
if ($this->hasRule('optional', $field) && isset($values)) {
|
if ($this->hasRule('optional', $field) && isset($values)) {
|
||||||
@ -981,7 +979,7 @@ class Validator
|
|||||||
} elseif (
|
} elseif (
|
||||||
$v['rule'] !== 'required' && !$this->hasRule('required', $field) &&
|
$v['rule'] !== 'required' && !$this->hasRule('required', $field) &&
|
||||||
$v['rule'] !== 'accepted' &&
|
$v['rule'] !== 'accepted' &&
|
||||||
(! isset($values) || $values === '' || ($multiple && count($values) == 0))
|
(!isset($values) || $values === '' || ($multiple && count($values) == 0))
|
||||||
) {
|
) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -996,6 +994,8 @@ class Validator
|
|||||||
|
|
||||||
if (!$multiple) {
|
if (!$multiple) {
|
||||||
$values = array($values);
|
$values = array($values);
|
||||||
|
} else if (! $this->hasRule('required', $field)){
|
||||||
|
$values = array_filter($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = true;
|
$result = true;
|
||||||
@ -1005,13 +1005,13 @@ class Validator
|
|||||||
|
|
||||||
if (!$result) {
|
if (!$result) {
|
||||||
$this->error($field, $v['message'], $v['params']);
|
$this->error($field, $v['message'], $v['params']);
|
||||||
if($this->stop_on_first_fail) {
|
if ($this->stop_on_first_fail) {
|
||||||
$set_to_break = true;
|
$set_to_break = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if($set_to_break) break;
|
if ($set_to_break) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return count($this->errors()) === 0;
|
return count($this->errors()) === 0;
|
||||||
@ -1021,8 +1021,9 @@ class Validator
|
|||||||
* Should the validation stop a rule is failed
|
* Should the validation stop a rule is failed
|
||||||
* @param bool $stop
|
* @param bool $stop
|
||||||
*/
|
*/
|
||||||
public function stopOnFirstFail($stop = true) {
|
public function stopOnFirstFail($stop = true)
|
||||||
$this->stop_on_first_fail = (bool) $stop;
|
{
|
||||||
|
$this->stop_on_first_fail = (bool)$stop;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1048,8 +1049,8 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Determine whether a field is being validated by the given rule.
|
* Determine whether a field is being validated by the given rule.
|
||||||
*
|
*
|
||||||
* @param string $name The name of the rule
|
* @param string $name The name of the rule
|
||||||
* @param string $field The name of the field
|
* @param string $field The name of the field
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@ -1078,9 +1079,9 @@ class Validator
|
|||||||
* Adds a new validation rule callback that is tied to the current
|
* Adds a new validation rule callback that is tied to the current
|
||||||
* instance only.
|
* instance only.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
* @param string $message
|
* @param string $message
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function addInstanceRule($name, $callback, $message = null)
|
public function addInstanceRule($name, $callback, $message = null)
|
||||||
@ -1094,15 +1095,14 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Register new validation rule callback
|
* Register new validation rule callback
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
* @param string $message
|
* @param string $message
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public static function addRule($name, $callback, $message = null)
|
public static function addRule($name, $callback, $message = null)
|
||||||
{
|
{
|
||||||
if ($message === null)
|
if ($message === null) {
|
||||||
{
|
|
||||||
$message = static::ERROR_DEFAULT;
|
$message = static::ERROR_DEFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1114,16 +1114,14 @@ class Validator
|
|||||||
|
|
||||||
public function getUniqueRuleName($fields)
|
public function getUniqueRuleName($fields)
|
||||||
{
|
{
|
||||||
if (is_array($fields))
|
if (is_array($fields)) {
|
||||||
{
|
|
||||||
$fields = implode("_", $fields);
|
$fields = implode("_", $fields);
|
||||||
}
|
}
|
||||||
|
|
||||||
$orgName = "{$fields}_rule";
|
$orgName = "{$fields}_rule";
|
||||||
$name = $orgName;
|
$name = $orgName;
|
||||||
$rules = $this->getRules();
|
$rules = $this->getRules();
|
||||||
while (isset($rules[$name]))
|
while (isset($rules[$name])) {
|
||||||
{
|
|
||||||
$name = $orgName . "_" . rand(0, 10000);
|
$name = $orgName . "_" . rand(0, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1134,7 +1132,7 @@ class Validator
|
|||||||
* Returns true if either a validator with the given name has been
|
* Returns true if either a validator with the given name has been
|
||||||
* registered or there is a default validator by that name.
|
* registered or there is a default validator by that name.
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function hasValidator($name)
|
public function hasValidator($name)
|
||||||
@ -1147,8 +1145,8 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Convenience method to add a single validation rule
|
* Convenience method to add a single validation rule
|
||||||
*
|
*
|
||||||
* @param string|callable $rule
|
* @param string|callable $rule
|
||||||
* @param array|string $fields
|
* @param array|string $fields
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
@ -1158,8 +1156,7 @@ class Validator
|
|||||||
$params = array_slice(func_get_args(), 2);
|
$params = array_slice(func_get_args(), 2);
|
||||||
|
|
||||||
if (is_callable($rule)
|
if (is_callable($rule)
|
||||||
&& !(is_string($rule) && $this->hasValidator($rule)))
|
&& !(is_string($rule) && $this->hasValidator($rule))) {
|
||||||
{
|
|
||||||
$name = $this->getUniqueRuleName($fields);
|
$name = $this->getUniqueRuleName($fields);
|
||||||
$msg = isset($params[0]) ? $params[0] : null;
|
$msg = isset($params[0]) ? $params[0] : null;
|
||||||
$this->addInstanceRule($name, $rule, $msg);
|
$this->addInstanceRule($name, $rule, $msg);
|
||||||
@ -1190,8 +1187,8 @@ class Validator
|
|||||||
|
|
||||||
$this->_validations[] = array(
|
$this->_validations[] = array(
|
||||||
'rule' => $rule,
|
'rule' => $rule,
|
||||||
'fields' => (array) $fields,
|
'fields' => (array)$fields,
|
||||||
'params' => (array) $params,
|
'params' => (array)$params,
|
||||||
'message' => $message
|
'message' => $message
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1216,7 +1213,7 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Add labels to rules
|
* Add labels to rules
|
||||||
*
|
*
|
||||||
* @param array $labels
|
* @param array $labels
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function labels($labels = array())
|
public function labels($labels = array())
|
||||||
@ -1229,7 +1226,7 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param string $msg
|
* @param string $msg
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function checkAndSetLabel($field, $msg, $params)
|
protected function checkAndSetLabel($field, $msg, $params)
|
||||||
@ -1240,7 +1237,7 @@ class Validator
|
|||||||
if (is_array($params)) {
|
if (is_array($params)) {
|
||||||
$i = 1;
|
$i = 1;
|
||||||
foreach ($params as $k => $v) {
|
foreach ($params as $k => $v) {
|
||||||
$tag = '{field'. $i .'}';
|
$tag = '{field' . $i . '}';
|
||||||
$label = isset($params[$k]) && (is_numeric($params[$k]) || is_string($params[$k])) && isset($this->_labels[$params[$k]]) ? $this->_labels[$params[$k]] : $tag;
|
$label = isset($params[$k]) && (is_numeric($params[$k]) || is_string($params[$k])) && isset($this->_labels[$params[$k]]) ? $this->_labels[$params[$k]] : $tag;
|
||||||
$msg = str_replace($tag, $label, $msg);
|
$msg = str_replace($tag, $label, $msg);
|
||||||
$i++;
|
$i++;
|
||||||
@ -1263,8 +1260,8 @@ class Validator
|
|||||||
foreach ($rules as $ruleType => $params) {
|
foreach ($rules as $ruleType => $params) {
|
||||||
if (is_array($params)) {
|
if (is_array($params)) {
|
||||||
foreach ($params as $innerParams) {
|
foreach ($params as $innerParams) {
|
||||||
if (! is_array($innerParams)){
|
if (!is_array($innerParams)) {
|
||||||
$innerParams = (array) $innerParams;
|
$innerParams = (array)$innerParams;
|
||||||
}
|
}
|
||||||
array_unshift($innerParams, $ruleType);
|
array_unshift($innerParams, $ruleType);
|
||||||
call_user_func_array(array($this, 'rule'), $innerParams);
|
call_user_func_array(array($this, 'rule'), $innerParams);
|
||||||
@ -1296,10 +1293,11 @@ class Validator
|
|||||||
* @param string field_name
|
* @param string field_name
|
||||||
* @param array $rules
|
* @param array $rules
|
||||||
*/
|
*/
|
||||||
public function mapFieldRules($field_name, $rules){
|
public function mapFieldRules($field_name, $rules)
|
||||||
|
{
|
||||||
$me = $this;
|
$me = $this;
|
||||||
|
|
||||||
array_map(function($rule) use($field_name, $me){
|
array_map(function ($rule) use ($field_name, $me) {
|
||||||
|
|
||||||
//rule must be an array
|
//rule must be an array
|
||||||
$rule = (array)$rule;
|
$rule = (array)$rule;
|
||||||
@ -1309,16 +1307,16 @@ class Validator
|
|||||||
|
|
||||||
//find a custom message, if any
|
//find a custom message, if any
|
||||||
$message = null;
|
$message = null;
|
||||||
if (isset($rule['message'])){
|
if (isset($rule['message'])) {
|
||||||
$message = $rule['message'];
|
$message = $rule['message'];
|
||||||
unset($rule['message']);
|
unset($rule['message']);
|
||||||
}
|
}
|
||||||
//Add the field and additional parameters to the rule
|
//Add the field and additional parameters to the rule
|
||||||
$added = call_user_func_array(array($me, 'rule'), array_merge(array($rule_name, $field_name), $rule));
|
$added = call_user_func_array(array($me, 'rule'), array_merge(array($rule_name, $field_name), $rule));
|
||||||
if (! empty($message)){
|
if (!empty($message)) {
|
||||||
$added->message($message);
|
$added->message($message);
|
||||||
}
|
}
|
||||||
}, (array) $rules);
|
}, (array)$rules);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1326,9 +1324,10 @@ class Validator
|
|||||||
*
|
*
|
||||||
* @param array $rules
|
* @param array $rules
|
||||||
*/
|
*/
|
||||||
public function mapFieldsRules($rules){
|
public function mapFieldsRules($rules)
|
||||||
|
{
|
||||||
$me = $this;
|
$me = $this;
|
||||||
array_map(function($field_name) use($rules, $me){
|
array_map(function ($field_name) use ($rules, $me) {
|
||||||
$me->mapFieldRules($field_name, $rules[$field_name]);
|
$me->mapFieldRules($field_name, $rules[$field_name]);
|
||||||
}, array_keys($rules));
|
}, array_keys($rules));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Valitron\Validator;
|
use Valitron\Validator;
|
||||||
|
|
||||||
class ValidateTest extends BaseTestCase
|
class ValidateTest extends BaseTestCase
|
||||||
@ -21,6 +22,7 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v->rule('required', 'name');
|
$v->rule('required', 'name');
|
||||||
$this->assertFalse($v->errors('name'));
|
$this->assertFalse($v->errors('name'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testArrayOfFieldsToValidate()
|
public function testArrayOfFieldsToValidate()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
||||||
@ -158,7 +160,8 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAcceptedNotSet(){
|
public function testAcceptedNotSet()
|
||||||
|
{
|
||||||
$v = new Validator();
|
$v = new Validator();
|
||||||
$v->rule('accepted', 'agree');
|
$v->rule('accepted', 'agree');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
@ -189,14 +192,15 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIntegerStrict(){
|
public function testIntegerStrict()
|
||||||
|
{
|
||||||
|
|
||||||
$v = new Validator(array('num' => ' 41243'));
|
$v = new Validator(array('num' => ' 41243'));
|
||||||
$v->rule('integer', 'num');
|
$v->rule('integer', 'num');
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
|
|
||||||
$v = new Validator(array('num' => ' 41243'));
|
$v = new Validator(array('num' => ' 41243'));
|
||||||
$v->rule('integer', 'num', true);
|
$v->rule('integer', 'num', true);
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
|
|
||||||
$v = new Validator(array('num' => '+41243'));
|
$v = new Validator(array('num' => '+41243'));
|
||||||
@ -645,13 +649,15 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmailDnsValid(){
|
public function testEmailDnsValid()
|
||||||
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
||||||
$v->rule('emailDNS', 'email');
|
$v->rule('emailDNS', 'email');
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testEmailDnsInvalid(){
|
public function testEmailDnsInvalid()
|
||||||
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.zyx'));
|
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.zyx'));
|
||||||
$v->rule('emailDNS', 'email');
|
$v->rule('emailDNS', 'email');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
@ -818,7 +824,7 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v->rule(
|
$v->rule(
|
||||||
'dateBefore',
|
'dateBefore',
|
||||||
'endDate',
|
'endDate',
|
||||||
new DateTime('2013-04-08')
|
new DateTime('2013-04-08')
|
||||||
)->label('End date')->message('{field} must be before the end of the fiscal year, %s.');
|
)->label('End date')->message('{field} must be before the end of the fiscal year, %s.');
|
||||||
|
|
||||||
$v->rule(
|
$v->rule(
|
||||||
@ -891,7 +897,7 @@ class ValidateTest extends BaseTestCase
|
|||||||
$rules = array(
|
$rules = array(
|
||||||
'required' => 'nonexistent_field',
|
'required' => 'nonexistent_field',
|
||||||
'accepted' => 'foo',
|
'accepted' => 'foo',
|
||||||
'integer' => 'foo'
|
'integer' => 'foo'
|
||||||
);
|
);
|
||||||
|
|
||||||
$v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz'));
|
$v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz'));
|
||||||
@ -937,7 +943,7 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testAcceptBulkRulesWithNestedRules()
|
public function testAcceptBulkRulesWithNestedRules()
|
||||||
{
|
{
|
||||||
$rules = array(
|
$rules = array(
|
||||||
'length' => array(
|
'length' => array(
|
||||||
array('foo', 5),
|
array('foo', 5),
|
||||||
array('bar', 5)
|
array('bar', 5)
|
||||||
)
|
)
|
||||||
@ -958,7 +964,7 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testAcceptBulkRulesWithNestedRulesAndMultipleFields()
|
public function testAcceptBulkRulesWithNestedRulesAndMultipleFields()
|
||||||
{
|
{
|
||||||
$rules = array(
|
$rules = array(
|
||||||
'length' => array(
|
'length' => array(
|
||||||
array(array('foo', 'bar'), 5),
|
array(array('foo', 'bar'), 5),
|
||||||
array('baz', 5)
|
array('baz', 5)
|
||||||
)
|
)
|
||||||
@ -979,7 +985,7 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testAcceptBulkRulesWithMultipleArrayParams()
|
public function testAcceptBulkRulesWithMultipleArrayParams()
|
||||||
{
|
{
|
||||||
$rules = array(
|
$rules = array(
|
||||||
'in' => array(
|
'in' => array(
|
||||||
array(array('foo', 'bar'), array('x', 'y'))
|
array(array('foo', 'bar'), array('x', 'y'))
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
@ -995,15 +1001,16 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertEquals($v1->errors(), $v2->errors());
|
$this->assertEquals($v1->errors(), $v2->errors());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMalformedBulkRules(){
|
public function testMalformedBulkRules()
|
||||||
$v = new Validator();
|
{
|
||||||
$v->rules(
|
$v = new Validator();
|
||||||
array(
|
$v->rules(
|
||||||
'required'=>array('foo', 'bar')
|
array(
|
||||||
)
|
'required' => array('foo', 'bar')
|
||||||
);
|
)
|
||||||
|
);
|
||||||
|
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCustomLabelInMessage()
|
public function testCustomLabelInMessage()
|
||||||
@ -1019,30 +1026,30 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v = new Valitron\Validator(array());
|
$v = new Valitron\Validator(array());
|
||||||
$v->rule('required', array('name', 'email'))->message('{field} is required');
|
$v->rule('required', array('name', 'email'))->message('{field} is required');
|
||||||
$v->labels(array(
|
$v->labels(array(
|
||||||
'name' => 'Name',
|
'name' => 'Name',
|
||||||
'email' => 'Email address'
|
'email' => 'Email address'
|
||||||
));
|
));
|
||||||
$v->validate();
|
$v->validate();
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'name' => array('Name is required'),
|
'name' => array('Name is required'),
|
||||||
'email' => array('Email address is required')
|
'email' => array('Email address is required')
|
||||||
), $v->errors());
|
), $v->errors());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCustomLabelArrayWithoutMessage()
|
public function testCustomLabelArrayWithoutMessage()
|
||||||
{
|
{
|
||||||
$v = new Valitron\Validator(array(
|
$v = new Valitron\Validator(array(
|
||||||
'password' => 'foo',
|
'password' => 'foo',
|
||||||
'passwordConfirm' => 'bar'
|
'passwordConfirm' => 'bar'
|
||||||
));
|
));
|
||||||
$v->rule('equals', 'password', 'passwordConfirm');
|
$v->rule('equals', 'password', 'passwordConfirm');
|
||||||
$v->labels(array(
|
$v->labels(array(
|
||||||
'password' => 'Password',
|
'password' => 'Password',
|
||||||
'passwordConfirm' => 'Password Confirm'
|
'passwordConfirm' => 'Password Confirm'
|
||||||
));
|
));
|
||||||
$v->validate();
|
$v->validate();
|
||||||
$this->assertEquals(array(
|
$this->assertEquals(array(
|
||||||
'password' => array("Password must be the same as 'Password Confirm'"),
|
'password' => array("Password must be the same as 'Password Confirm'"),
|
||||||
), $v->errors());
|
), $v->errors());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1053,7 +1060,9 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testAddRuleClosure()
|
public function testAddRuleClosure()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester'));
|
$v = new Validator(array('name' => 'Chester Tester'));
|
||||||
$v->addRule('testRule', function() { return true; });
|
$v->addRule('testRule', function () {
|
||||||
|
return true;
|
||||||
|
});
|
||||||
$v->rule('testRule', 'name');
|
$v->rule('testRule', 'name');
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
@ -1061,7 +1070,9 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testAddRuleClosureReturnsFalse()
|
public function testAddRuleClosureReturnsFalse()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester'));
|
$v = new Validator(array('name' => 'Chester Tester'));
|
||||||
$v->addRule('testRule', function() { return false; });
|
$v->addRule('testRule', function () {
|
||||||
|
return false;
|
||||||
|
});
|
||||||
$v->rule('testRule', 'name');
|
$v->rule('testRule', 'name');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
@ -1069,7 +1080,9 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testAddRuleClosureWithFieldArray()
|
public function testAddRuleClosureWithFieldArray()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'foo@example.com'));
|
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'foo@example.com'));
|
||||||
$v->addRule('testRule', function() { return true; });
|
$v->addRule('testRule', function () {
|
||||||
|
return true;
|
||||||
|
});
|
||||||
$v->rule('testRule', array('name', 'email'));
|
$v->rule('testRule', array('name', 'email'));
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
@ -1077,7 +1090,9 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testAddRuleClosureWithArrayAsExtraParameter()
|
public function testAddRuleClosureWithArrayAsExtraParameter()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester'));
|
$v = new Validator(array('name' => 'Chester Tester'));
|
||||||
$v->addRule('testRule', function() { return true; });
|
$v->addRule('testRule', function () {
|
||||||
|
return true;
|
||||||
|
});
|
||||||
$v->rule('testRule', 'name', array('foo', 'bar'));
|
$v->rule('testRule', 'name', array('foo', 'bar'));
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
@ -1090,8 +1105,16 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sampleObjectCallback() { return true; }
|
public function sampleObjectCallback()
|
||||||
public function sampleObjectCallbackFalse() { return false; }
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sampleObjectCallbackFalse()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public function testAddRuleCallbackArray()
|
public function testAddRuleCallbackArray()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester'));
|
$v = new Validator(array('name' => 'Chester Tester'));
|
||||||
@ -1155,14 +1178,14 @@ class ValidateTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testCreditCardValid()
|
public function testCreditCardValid()
|
||||||
{
|
{
|
||||||
$visa = array(4539511619543489, 4532949059629052, 4024007171194938, 4929646403373269, 4539135861690622);
|
$visa = array(4539511619543489, 4532949059629052, 4024007171194938, 4929646403373269, 4539135861690622);
|
||||||
$mastercard = array(5162057048081965, 5382687859049349, 5484388880142230, 5464941521226434, 5473481232685965, 2223000048400011, 2223520043560014);
|
$mastercard = array(5162057048081965, 5382687859049349, 5484388880142230, 5464941521226434, 5473481232685965, 2223000048400011, 2223520043560014);
|
||||||
$amex = array(371442067262027, 340743030537918, 345509167493596, 343665795576848, 346087552944316);
|
$amex = array(371442067262027, 340743030537918, 345509167493596, 343665795576848, 346087552944316);
|
||||||
$dinersclub = array(30363194756249, 30160097740704, 38186521192206, 38977384214552, 38563220301454);
|
$dinersclub = array(30363194756249, 30160097740704, 38186521192206, 38977384214552, 38563220301454);
|
||||||
$discover = array(6011712400392605, 6011536340491809, 6011785775263015, 6011984124619056, 6011320958064251);
|
$discover = array(6011712400392605, 6011536340491809, 6011785775263015, 6011984124619056, 6011320958064251);
|
||||||
|
|
||||||
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
|
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
|
||||||
foreach($numbers as $number) {
|
foreach ($numbers as $number) {
|
||||||
$v = new Validator(array('test' => $number));
|
$v = new Validator(array('test' => $number));
|
||||||
$v->rule('creditCard', 'test');
|
$v->rule('creditCard', 'test');
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
@ -1179,14 +1202,14 @@ class ValidateTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testCreditCardInvalid()
|
public function testCreditCardInvalid()
|
||||||
{
|
{
|
||||||
$visa = array(3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622);
|
$visa = array(3539511619543489, 3532949059629052, 3024007171194938, 3929646403373269, 3539135861690622);
|
||||||
$mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965);
|
$mastercard = array(4162057048081965, 4382687859049349, 4484388880142230, 4464941521226434, 4473481232685965);
|
||||||
$amex = array(271442067262027, 240743030537918, 245509167493596, 243665795576848, 246087552944316);
|
$amex = array(271442067262027, 240743030537918, 245509167493596, 243665795576848, 246087552944316);
|
||||||
$dinersclub = array(20363194756249, 20160097740704, 28186521192206, 28977384214552, 28563220301454);
|
$dinersclub = array(20363194756249, 20160097740704, 28186521192206, 28977384214552, 28563220301454);
|
||||||
$discover = array(5011712400392605, 5011536340491809, 5011785775263015, 5011984124619056, 5011320958064251);
|
$discover = array(5011712400392605, 5011536340491809, 5011785775263015, 5011984124619056, 5011320958064251);
|
||||||
|
|
||||||
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
|
foreach (compact('visa', 'mastercard', 'amex', 'dinersclub', 'discover') as $type => $numbers) {
|
||||||
foreach($numbers as $number) {
|
foreach ($numbers as $number) {
|
||||||
$v = new Validator(array('test' => $number));
|
$v = new Validator(array('test' => $number));
|
||||||
$v->rule('creditCard', 'test');
|
$v->rule('creditCard', 'test');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
@ -1207,66 +1230,66 @@ class ValidateTest extends BaseTestCase
|
|||||||
|
|
||||||
public function testInstanceOfValidWithString()
|
public function testInstanceOfValidWithString()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('attributeName' => new stdClass()));
|
$v = new Validator(array('attributeName' => new stdClass()));
|
||||||
$v->rule('instanceOf', 'attributeName', 'stdClass');
|
$v->rule('instanceOf', 'attributeName', 'stdClass');
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOfInvalidWithInstance()
|
public function testInstanceOfInvalidWithInstance()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('attributeName' => new stdClass()));
|
$v = new Validator(array('attributeName' => new stdClass()));
|
||||||
$v->rule('instanceOf', 'attributeName', new Validator(array()));
|
$v->rule('instanceOf', 'attributeName', new Validator(array()));
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOfValidWithInstance()
|
public function testInstanceOfValidWithInstance()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('attributeName' => new stdClass()));
|
$v = new Validator(array('attributeName' => new stdClass()));
|
||||||
$v->rule('instanceOf', 'attributeName', new stdClass());
|
$v->rule('instanceOf', 'attributeName', new stdClass());
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOfErrorMessageShowsInstanceName()
|
public function testInstanceOfErrorMessageShowsInstanceName()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('attributeName' => new Validator(array())));
|
$v = new Validator(array('attributeName' => new Validator(array())));
|
||||||
$v->rule('instanceOf', 'attributeName', new stdClass());
|
$v->rule('instanceOf', 'attributeName', new stdClass());
|
||||||
$v->validate();
|
$v->validate();
|
||||||
$expected_error = array(
|
$expected_error = array(
|
||||||
"attributeName" => array(
|
"attributeName" => array(
|
||||||
"AttributeName must be an instance of 'stdClass'"
|
"AttributeName must be an instance of 'stdClass'"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$this->assertEquals($expected_error, $v->errors());
|
$this->assertEquals($expected_error, $v->errors());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOfInvalidWithString()
|
public function testInstanceOfInvalidWithString()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('attributeName' => new stdClass()));
|
$v = new Validator(array('attributeName' => new stdClass()));
|
||||||
$v->rule('instanceOf', 'attributeName', 'SomeOtherClass');
|
$v->rule('instanceOf', 'attributeName', 'SomeOtherClass');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOfWithAlternativeSyntaxValid()
|
public function testInstanceOfWithAlternativeSyntaxValid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('attributeName' => new stdClass()));
|
$v = new Validator(array('attributeName' => new stdClass()));
|
||||||
$v->rules(array(
|
$v->rules(array(
|
||||||
'instanceOf' => array(
|
'instanceOf' => array(
|
||||||
array('attributeName', 'stdClass')
|
array('attributeName', 'stdClass')
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testInstanceOfWithAlternativeSyntaxInvalid()
|
public function testInstanceOfWithAlternativeSyntaxInvalid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('attributeName' => new stdClass()));
|
$v = new Validator(array('attributeName' => new stdClass()));
|
||||||
$v->rules(array(
|
$v->rules(array(
|
||||||
'instanceOf' => array(
|
'instanceOf' => array(
|
||||||
array('attributeName', 'SomeOtherClassInAlternativeSyntaxInvalid')
|
array('attributeName', 'SomeOtherClassInAlternativeSyntaxInvalid')
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
$v->validate();
|
$v->validate();
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1285,29 +1308,29 @@ class ValidateTest extends BaseTestCase
|
|||||||
return array(
|
return array(
|
||||||
array(
|
array(
|
||||||
'expected' => 'Test must be at least 140 long',
|
'expected' => 'Test must be at least 140 long',
|
||||||
'input' => 'tweeet',
|
'input' => 'tweeet',
|
||||||
'test' => array(140),
|
'test' => array(140),
|
||||||
'message' => '{field} must be at least %d long',
|
'message' => '{field} must be at least %d long',
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'expected' => 'Test must be between 1 and 140 characters',
|
'expected' => 'Test must be between 1 and 140 characters',
|
||||||
'input' => array(1, 2, 3),
|
'input' => array(1, 2, 3),
|
||||||
'test' => array(1, 140),
|
'test' => array(1, 140),
|
||||||
'message' => 'Test must be between %d and %d characters',
|
'message' => 'Test must be between %d and %d characters',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testOptionalProvidedValid()
|
public function testOptionalProvidedValid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('address' => 'user@example.com'));
|
$v = new Validator(array('address' => 'user@example.com'));
|
||||||
$v->rule('optional', 'address')->rule('email', 'address');
|
$v->rule('optional', 'address')->rule('email', 'address');
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testOptionalProvidedInvalid()
|
public function testOptionalProvidedInvalid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('address' => 'userexample.com'));
|
$v = new Validator(array('address' => 'userexample.com'));
|
||||||
$v->rule('optional', 'address')->rule('email', 'address');
|
$v->rule('optional', 'address')->rule('email', 'address');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
@ -1341,30 +1364,31 @@ class ValidateTest extends BaseTestCase
|
|||||||
public function testRequiredEdgeCases()
|
public function testRequiredEdgeCases()
|
||||||
{
|
{
|
||||||
$v = new Validator(array(
|
$v = new Validator(array(
|
||||||
'zero'=>0,
|
'zero' => 0,
|
||||||
'zero_txt' => '0',
|
'zero_txt' => '0',
|
||||||
'false'=>false,
|
'false' => false,
|
||||||
'empty_array'=>array()
|
'empty_array' => array()
|
||||||
));
|
));
|
||||||
$v->rule('required', array('zero', 'zero_txt', 'false', 'empty_array'));
|
$v->rule('required', array('zero', 'zero_txt', 'false', 'empty_array'));
|
||||||
|
|
||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRequiredAllowEmpty(){
|
public function testRequiredAllowEmpty()
|
||||||
$data= array(
|
{
|
||||||
'empty_text'=>'',
|
$data = array(
|
||||||
|
'empty_text' => '',
|
||||||
'null_value' => null,
|
'null_value' => null,
|
||||||
'in_array'=>array(
|
'in_array' => array(
|
||||||
'empty_text'=>''
|
'empty_text' => ''
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$v1= new Validator($data);
|
$v1 = new Validator($data);
|
||||||
$v1->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'));
|
$v1->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'));
|
||||||
$this->assertFalse($v1->validate());
|
$this->assertFalse($v1->validate());
|
||||||
|
|
||||||
$v2= new Validator($data);
|
$v2 = new Validator($data);
|
||||||
$v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'), true);
|
$v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'), true);
|
||||||
$this->assertTrue($v2->validate());
|
$this->assertTrue($v2->validate());
|
||||||
}
|
}
|
||||||
@ -1457,8 +1481,41 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://github.com/vlucas/valitron/issues/262
|
||||||
|
*/
|
||||||
|
public function testOptionalArrayPartsAreIgnored()
|
||||||
|
{
|
||||||
|
$v = new Validator(array(
|
||||||
|
'data' => array(
|
||||||
|
array('foo' => '2018-01-01'),
|
||||||
|
array('bar' => 1)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$v->rule('date', 'data.*.foo');
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://github.com/vlucas/valitron/issues/262
|
||||||
|
*/
|
||||||
|
public function testRequiredArrayPartsAreNotIgnored()
|
||||||
|
{
|
||||||
|
$v = new Validator(array(
|
||||||
|
'data' => array(
|
||||||
|
array('foo' => '2018-01-01'),
|
||||||
|
array('bar' => 1)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$v->rule('required', 'data.*.foo');
|
||||||
|
$v->rule('date', 'data.*.foo');
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function sampleFunctionCallback($field, $value, array $params) {
|
function sampleFunctionCallback($field, $value, array $params)
|
||||||
return true;
|
{
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user