forked from devsam/core
composer package update
This commit is contained in:
+362
-149
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Valitron;
|
||||
|
||||
/**
|
||||
@@ -85,10 +86,10 @@ class Validator
|
||||
/**
|
||||
* Setup validation
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $fields
|
||||
* @param string $lang
|
||||
* @param string $langDir
|
||||
* @param array $data
|
||||
* @param array $fields
|
||||
* @param string $lang
|
||||
* @param string $langDir
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct($data = array(), $fields = array(), $lang = null, $langDir = null)
|
||||
@@ -105,7 +106,7 @@ class Validator
|
||||
|
||||
// Load language file in directory
|
||||
$langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
|
||||
if (stream_resolve_include_path($langFile) ) {
|
||||
if (stream_resolve_include_path($langFile)) {
|
||||
$langMessages = include $langFile;
|
||||
static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
|
||||
} else {
|
||||
@@ -147,13 +148,13 @@ class Validator
|
||||
* Required field validator
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @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);
|
||||
return $find[1];
|
||||
}
|
||||
@@ -173,7 +174,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateEquals($field, $value, array $params)
|
||||
@@ -189,7 +189,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateDifferent($field, $value, array $params)
|
||||
@@ -205,7 +204,7 @@ class Validator
|
||||
* This validation rule implies the field is "required"
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateAccepted($field, $value)
|
||||
@@ -219,7 +218,7 @@ class Validator
|
||||
* Validate that a field is an array
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateArray($field, $value)
|
||||
@@ -231,7 +230,7 @@ class Validator
|
||||
* Validate that a field is numeric
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateNumeric($field, $value)
|
||||
@@ -243,13 +242,13 @@ class Validator
|
||||
* Validate that a field is an integer
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateInteger($field, $value, $params)
|
||||
{
|
||||
if (isset($params[0]) && (bool) $params[0]){
|
||||
if (isset($params[0]) && (bool)$params[0]) {
|
||||
//strict mode
|
||||
return preg_match('/^([0-9]|-[1-9]|-?[1-9][0-9]*)$/i', $value);
|
||||
}
|
||||
@@ -263,7 +262,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateLength($field, $value, $params)
|
||||
@@ -283,7 +281,7 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateLengthBetween($field, $value, $params)
|
||||
{
|
||||
@@ -296,10 +294,10 @@ class Validator
|
||||
* Validate the length of a string (min)
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
*
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateLengthMin($field, $value, $params)
|
||||
{
|
||||
@@ -312,10 +310,10 @@ class Validator
|
||||
* Validate the length of a string (max)
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
*
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateLengthMax($field, $value, $params)
|
||||
{
|
||||
@@ -347,7 +345,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateMin($field, $value, $params)
|
||||
@@ -367,7 +364,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateMax($field, $value, $params)
|
||||
@@ -385,9 +381,8 @@ class Validator
|
||||
* Validate the size of a field is between min and max values
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateBetween($field, $value, $params)
|
||||
@@ -410,7 +405,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateIn($field, $value, $params)
|
||||
@@ -428,13 +422,35 @@ class Validator
|
||||
return in_array($value, $params[0], $strict);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a field is contained within a list of values
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateListContains($field, $value, $params)
|
||||
{
|
||||
$isAssoc = array_values($value) !== $value;
|
||||
if ($isAssoc) {
|
||||
$value = array_keys($value);
|
||||
}
|
||||
|
||||
$strict = false;
|
||||
if (isset($params[1])) {
|
||||
$strict = $params[1];
|
||||
}
|
||||
|
||||
return in_array($params[0], $value, $strict);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a field is not contained within a list of values
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateNotIn($field, $value, $params)
|
||||
@@ -446,7 +462,7 @@ class Validator
|
||||
* Validate a field contains a given string
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param string $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
@@ -461,10 +477,9 @@ class Validator
|
||||
|
||||
$strict = true;
|
||||
if (isset($params[1])) {
|
||||
$strict = (bool) $params[1];
|
||||
$strict = (bool)$params[1];
|
||||
}
|
||||
|
||||
$isContains = false;
|
||||
if ($strict) {
|
||||
if (function_exists('mb_strpos')) {
|
||||
$isContains = mb_strpos($value, $params[0]) !== false;
|
||||
@@ -481,11 +496,51 @@ class Validator
|
||||
return $isContains;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that all field values contains a given array
|
||||
*
|
||||
* @param string $field
|
||||
* @param array $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateSubset($field, $value, $params)
|
||||
{
|
||||
if (!isset($params[0])) {
|
||||
return false;
|
||||
}
|
||||
if (!is_array($params[0])) {
|
||||
$params[0] = array($params[0]);
|
||||
}
|
||||
if (is_scalar($value) || is_null($value)) {
|
||||
return $this->validateIn($field, $value, $params);
|
||||
}
|
||||
|
||||
$intersect = array_intersect($value, $params[0]);
|
||||
return array_diff($value, $intersect) === array_diff($intersect, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that field array has only unique values
|
||||
*
|
||||
* @param string $field
|
||||
* @param array $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateContainsUnique($field, $value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $value === array_unique($value, SORT_REGULAR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid IP address
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateIp($field, $value)
|
||||
@@ -494,22 +549,64 @@ class Validator
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid e-mail address
|
||||
* Validate that a field is a valid IP v4 address
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateIpv4($field, $value)
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid IP v6 address
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateIpv6($field, $value)
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid e-mail address
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateEmail($field, $value)
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field contains only ASCII characters
|
||||
*
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool|false|string
|
||||
*/
|
||||
protected function validateAscii($field, $value)
|
||||
{
|
||||
// multibyte extension needed
|
||||
if (function_exists('mb_detect_encoding')) {
|
||||
return mb_detect_encoding($value, 'ASCII', true);
|
||||
}
|
||||
|
||||
// fallback with regex
|
||||
return 0 === preg_match('/[^\x00-\x7F]/', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid e-mail address and the domain name is active
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateEmailDNS($field, $value)
|
||||
@@ -529,7 +626,7 @@ class Validator
|
||||
* Validate that a field is a valid URL by syntax
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateUrl($field, $value)
|
||||
@@ -547,7 +644,7 @@ class Validator
|
||||
* Validate that a field is an active URL by verifying DNS record
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateUrlActive($field, $value)
|
||||
@@ -567,7 +664,7 @@ class Validator
|
||||
* Validate that a field contains only alphabetic characters
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateAlpha($field, $value)
|
||||
@@ -579,7 +676,7 @@ class Validator
|
||||
* Validate that a field contains only alpha-numeric characters
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateAlphaNum($field, $value)
|
||||
@@ -591,12 +688,12 @@ class Validator
|
||||
* Validate that a field contains only alpha-numeric characters, dashes, and underscores
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateSlug($field, $value)
|
||||
{
|
||||
if(is_array($value)) {
|
||||
if (is_array($value)) {
|
||||
return false;
|
||||
}
|
||||
return preg_match('/^([-a-z0-9_-])+$/i', $value);
|
||||
@@ -606,8 +703,8 @@ class Validator
|
||||
* Validate that a field passes a regular expression check
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateRegex($field, $value, $params)
|
||||
@@ -619,7 +716,7 @@ class Validator
|
||||
* Validate that a field is a valid date
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateDate($field, $value)
|
||||
@@ -640,7 +737,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateDateFormat($field, $value, $params)
|
||||
@@ -656,7 +752,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateDateBefore($field, $value, $params)
|
||||
@@ -673,7 +768,6 @@ class Validator
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @internal param array $fields
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateDateAfter($field, $value, $params)
|
||||
@@ -688,7 +782,7 @@ class Validator
|
||||
* Validate that a field contains a boolean.
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateBoolean($field, $value)
|
||||
@@ -701,8 +795,8 @@ class Validator
|
||||
* optionally filtered by an array
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateCreditCard($field, $value, $params)
|
||||
@@ -718,7 +812,7 @@ class Validator
|
||||
if (is_array($params[0])) {
|
||||
$cards = $params[0];
|
||||
} elseif (is_string($params[0])) {
|
||||
$cardType = $params[0];
|
||||
$cardType = $params[0];
|
||||
if (isset($params[1]) && is_array($params[1])) {
|
||||
$cards = $params[1];
|
||||
if (!in_array($cardType, $cards)) {
|
||||
@@ -741,7 +835,7 @@ class Validator
|
||||
return false;
|
||||
}
|
||||
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) {
|
||||
$sub_total = $digit * 2;
|
||||
if ($sub_total > 9) {
|
||||
@@ -753,7 +847,7 @@ class Validator
|
||||
$sum += $sub_total;
|
||||
}
|
||||
if ($sum > 0 && $sum % 10 == 0) {
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -764,11 +858,11 @@ class Validator
|
||||
return true;
|
||||
} else {
|
||||
$cardRegex = array(
|
||||
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
|
||||
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
|
||||
'amex' => '#^3[47][0-9]{13}$#',
|
||||
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
|
||||
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
|
||||
'visa' => '#^4[0-9]{12}(?:[0-9]{3})?$#',
|
||||
'mastercard' => '#^(5[1-5]|2[2-7])[0-9]{14}$#',
|
||||
'amex' => '#^3[47][0-9]{13}$#',
|
||||
'dinersclub' => '#^3(?:0[0-5]|[68][0-9])[0-9]{11}$#',
|
||||
'discover' => '#^6(?:011|5[0-9]{2})[0-9]{12}$#',
|
||||
);
|
||||
|
||||
if (isset($cardType)) {
|
||||
@@ -826,14 +920,127 @@ class Validator
|
||||
return $isInstanceOf;
|
||||
}
|
||||
|
||||
//Validate optional field
|
||||
protected function validateOptional($field, $value, $params) {
|
||||
//Always return true
|
||||
/**
|
||||
* Validates whether or not a field is required based on whether or not other fields are present.
|
||||
*
|
||||
* @param string $field name of the field in the data array
|
||||
* @param mixed $value value of this field
|
||||
* @param array $params parameters for this rule
|
||||
* @param array $fields full list of data to be validated
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateRequiredWith($field, $value, $params, $fields)
|
||||
{
|
||||
$conditionallyReq = false;
|
||||
// if we actually have conditionally required with fields to check against
|
||||
if (isset($params[0])) {
|
||||
// convert single value to array if it isn't already
|
||||
$reqParams = is_array($params[0]) ? $params[0] : array($params[0]);
|
||||
// check for the flag indicating if all fields are required
|
||||
$allRequired = isset($params[1]) && (bool)$params[1];
|
||||
$emptyFields = 0;
|
||||
foreach ($reqParams as $requiredField) {
|
||||
// check the field is set, not null, and not the empty string
|
||||
if (isset($fields[$requiredField]) && !is_null($fields[$requiredField])
|
||||
&& (is_string($fields[$requiredField]) ? trim($fields[$requiredField]) !== '' : true)) {
|
||||
if (!$allRequired) {
|
||||
$conditionallyReq = true;
|
||||
break;
|
||||
} else {
|
||||
$emptyFields++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if all required fields are present in strict mode, we're requiring it
|
||||
if ($allRequired && $emptyFields === count($reqParams)) {
|
||||
$conditionallyReq = true;
|
||||
}
|
||||
}
|
||||
// if we have conditionally required fields
|
||||
if ($conditionallyReq && (is_null($value) ||
|
||||
is_string($value) && trim($value) === '')) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get array of fields and data
|
||||
* Validates whether or not a field is required based on whether or not other fields are present.
|
||||
*
|
||||
* @param string $field name of the field in the data array
|
||||
* @param mixed $value value of this field
|
||||
* @param array $params parameters for this rule
|
||||
* @param array $fields full list of data to be validated
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateRequiredWithout($field, $value, $params, $fields)
|
||||
{
|
||||
$conditionallyReq = false;
|
||||
// if we actually have conditionally required with fields to check against
|
||||
if (isset($params[0])) {
|
||||
// convert single value to array if it isn't already
|
||||
$reqParams = is_array($params[0]) ? $params[0] : array($params[0]);
|
||||
// check for the flag indicating if all fields are required
|
||||
$allEmpty = isset($params[1]) && (bool)$params[1];
|
||||
$filledFields = 0;
|
||||
foreach ($reqParams as $requiredField) {
|
||||
// check the field is NOT set, null, or the empty string, in which case we are requiring this value be present
|
||||
if (!isset($fields[$requiredField]) || (is_null($fields[$requiredField])
|
||||
|| (is_string($fields[$requiredField]) && trim($fields[$requiredField]) === ''))) {
|
||||
if (!$allEmpty) {
|
||||
$conditionallyReq = true;
|
||||
break;
|
||||
} else {
|
||||
$filledFields++;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if all fields were empty, then we're requiring this in strict mode
|
||||
if ($allEmpty && $filledFields === count($reqParams)) {
|
||||
$conditionallyReq = true;
|
||||
}
|
||||
}
|
||||
// if we have conditionally required fields
|
||||
if ($conditionallyReq && (is_null($value) ||
|
||||
is_string($value) && trim($value) === '')) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate optional field
|
||||
*
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @param $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateOptional($field, $value, $params)
|
||||
{
|
||||
//Always return true
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function validateArrayHasKeys($field, $value, $params)
|
||||
{
|
||||
if (!is_array($value) || !isset($params[0])) {
|
||||
return false;
|
||||
}
|
||||
$requiredFields = $params[0];
|
||||
if (count($requiredFields) === 0) {
|
||||
return false;
|
||||
}
|
||||
foreach ($requiredFields as $fieldName) {
|
||||
if (!array_key_exists($fieldName, $value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get array of fields and data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
@@ -861,12 +1068,12 @@ class Validator
|
||||
* Add an error to error messages array
|
||||
*
|
||||
* @param string $field
|
||||
* @param string $msg
|
||||
* @param string $message
|
||||
* @param array $params
|
||||
*/
|
||||
public function error($field, $msg, array $params = array())
|
||||
public function error($field, $message, array $params = array())
|
||||
{
|
||||
$msg = $this->checkAndSetLabel($field, $msg, $params);
|
||||
$message = $this->checkAndSetLabel($field, $message, $params);
|
||||
|
||||
$values = array();
|
||||
// Printed values need to be in string format
|
||||
@@ -890,18 +1097,18 @@ class Validator
|
||||
$values[] = $param;
|
||||
}
|
||||
|
||||
$this->_errors[$field][] = vsprintf($msg, $values);
|
||||
$this->_errors[$field][] = vsprintf($message, $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify validation message to use for error for the last validation rule
|
||||
*
|
||||
* @param string $msg
|
||||
* @return $this
|
||||
* @param string $message
|
||||
* @return Validator
|
||||
*/
|
||||
public function message($msg)
|
||||
public function message($message)
|
||||
{
|
||||
$this->_validations[count($this->_validations) - 1]['message'] = $msg;
|
||||
$this->_validations[count($this->_validations) - 1]['message'] = $message;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -925,7 +1132,7 @@ class Validator
|
||||
}
|
||||
// Catches the case where the data isn't an array or object
|
||||
if (is_scalar($data)) {
|
||||
return array(NULL, false);
|
||||
return array(null, false);
|
||||
}
|
||||
$identifier = array_shift($identifiers);
|
||||
// Glob match
|
||||
@@ -940,24 +1147,21 @@ class Validator
|
||||
}
|
||||
}
|
||||
return array($values, true);
|
||||
}
|
||||
// Dead end, abort
|
||||
elseif ($identifier === NULL || ! isset($data[$identifier])) {
|
||||
} // Dead end, abort
|
||||
elseif ($identifier === null || ! isset($data[$identifier])) {
|
||||
if ($allow_empty){
|
||||
//when empty values are allowed, we only care if the key exists
|
||||
return array(null, array_key_exists($identifier, $data));
|
||||
}
|
||||
return array(null, false);
|
||||
}
|
||||
// Match array element
|
||||
} // Match array element
|
||||
elseif (count($identifiers) === 0) {
|
||||
if ($allow_empty){
|
||||
if ($allow_empty) {
|
||||
//when empty values are allowed, we only care if the key exists
|
||||
return array(null, array_key_exists($identifier, $data));
|
||||
}
|
||||
return array($data[$identifier], false);
|
||||
}
|
||||
// We need to go deeper
|
||||
return array($data[$identifier], $allow_empty);
|
||||
} // We need to go deeper
|
||||
else {
|
||||
return $this->getPart($data[$identifier], $identifiers, $allow_empty);
|
||||
}
|
||||
@@ -966,22 +1170,23 @@ class Validator
|
||||
/**
|
||||
* Run validations and return boolean result
|
||||
*
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
$set_to_break = false;
|
||||
$set_to_break = false;
|
||||
foreach ($this->_validations as $v) {
|
||||
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
|
||||
if ($this->hasRule('optional', $field) && isset($values)) {
|
||||
// Don't validate if the field is not required and the value is empty and we don't have a conditionally required rule present on the field
|
||||
if (($this->hasRule('optional', $field) && isset($values))
|
||||
|| ($this->hasRule('requiredWith', $field) || $this->hasRule('requiredWithout', $field))) {
|
||||
//Continue with execution below if statement
|
||||
} elseif (
|
||||
$v['rule'] !== 'required' && !$this->hasRule('required', $field) &&
|
||||
$v['rule'] !== 'accepted' &&
|
||||
(! isset($values) || $values === '' || ($multiple && count($values) == 0))
|
||||
(!isset($values) || $values === '' || ($multiple && count($values) == 0))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
@@ -996,6 +1201,8 @@ class Validator
|
||||
|
||||
if (!$multiple) {
|
||||
$values = array($values);
|
||||
} else if (! $this->hasRule('required', $field)){
|
||||
$values = array_filter($values);
|
||||
}
|
||||
|
||||
$result = true;
|
||||
@@ -1005,13 +1212,15 @@ class Validator
|
||||
|
||||
if (!$result) {
|
||||
$this->error($field, $v['message'], $v['params']);
|
||||
if($this->stop_on_first_fail) {
|
||||
$set_to_break = true;
|
||||
break;
|
||||
if ($this->stop_on_first_fail) {
|
||||
$set_to_break = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if($set_to_break) break;
|
||||
if ($set_to_break) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return count($this->errors()) === 0;
|
||||
@@ -1021,8 +1230,9 @@ class Validator
|
||||
* Should the validation stop a rule is failed
|
||||
* @param bool $stop
|
||||
*/
|
||||
public function stopOnFirstFail($stop = true) {
|
||||
$this->stop_on_first_fail = (bool) $stop;
|
||||
public function stopOnFirstFail($stop = true)
|
||||
{
|
||||
$this->stop_on_first_fail = (bool)$stop;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1050,9 +1260,8 @@ class Validator
|
||||
*
|
||||
* @param string $name The name of the rule
|
||||
* @param string $field The name of the field
|
||||
* @return boolean
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
protected function hasRule($name, $field)
|
||||
{
|
||||
foreach ($this->_validations as $validation) {
|
||||
@@ -1069,18 +1278,19 @@ class Validator
|
||||
protected static function assertRuleCallback($callback)
|
||||
{
|
||||
if (!is_callable($callback)) {
|
||||
throw new \InvalidArgumentException('Second argument must be a valid callback. Given argument was not callable.');
|
||||
throw new \InvalidArgumentException(
|
||||
'Second argument must be a valid callback. Given argument was not callable.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds a new validation rule callback that is tied to the current
|
||||
* instance only.
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $callback
|
||||
* @param string $message
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @param string $message
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function addInstanceRule($name, $callback, $message = null)
|
||||
@@ -1094,15 +1304,14 @@ class Validator
|
||||
/**
|
||||
* Register new validation rule callback
|
||||
*
|
||||
* @param string $name
|
||||
* @param mixed $callback
|
||||
* @param string $message
|
||||
* @param string $name
|
||||
* @param callable $callback
|
||||
* @param string $message
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function addRule($name, $callback, $message = null)
|
||||
{
|
||||
if ($message === null)
|
||||
{
|
||||
if ($message === null) {
|
||||
$message = static::ERROR_DEFAULT;
|
||||
}
|
||||
|
||||
@@ -1112,18 +1321,20 @@ class Validator
|
||||
static::$_ruleMessages[$name] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $fields
|
||||
* @return string
|
||||
*/
|
||||
public function getUniqueRuleName($fields)
|
||||
{
|
||||
if (is_array($fields))
|
||||
{
|
||||
if (is_array($fields)) {
|
||||
$fields = implode("_", $fields);
|
||||
}
|
||||
|
||||
$orgName = "{$fields}_rule";
|
||||
$name = $orgName;
|
||||
$rules = $this->getRules();
|
||||
while (isset($rules[$name]))
|
||||
{
|
||||
while (isset($rules[$name])) {
|
||||
$name = $orgName . "_" . rand(0, 10000);
|
||||
}
|
||||
|
||||
@@ -1134,7 +1345,7 @@ class Validator
|
||||
* Returns true if either a validator with the given name has been
|
||||
* registered or there is a default validator by that name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasValidator($name)
|
||||
@@ -1147,9 +1358,9 @@ class Validator
|
||||
/**
|
||||
* Convenience method to add a single validation rule
|
||||
*
|
||||
* @param string|callback $rule
|
||||
* @param array|string $fields
|
||||
* @return $this
|
||||
* @param string|callable $rule
|
||||
* @param array|string $fields
|
||||
* @return Validator
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function rule($rule, $fields)
|
||||
@@ -1158,11 +1369,10 @@ class Validator
|
||||
$params = array_slice(func_get_args(), 2);
|
||||
|
||||
if (is_callable($rule)
|
||||
&& !(is_string($rule) && $this->hasValidator($rule)))
|
||||
{
|
||||
&& !(is_string($rule) && $this->hasValidator($rule))) {
|
||||
$name = $this->getUniqueRuleName($fields);
|
||||
$msg = isset($params[0]) ? $params[0] : null;
|
||||
$this->addInstanceRule($name, $rule, $msg);
|
||||
$message = isset($params[0]) ? $params[0] : null;
|
||||
$this->addInstanceRule($name, $rule, $message);
|
||||
$rule = $name;
|
||||
}
|
||||
|
||||
@@ -1170,13 +1380,15 @@ class Validator
|
||||
if (!isset($errors[$rule])) {
|
||||
$ruleMethod = 'validate' . ucfirst($rule);
|
||||
if (!method_exists($this, $ruleMethod)) {
|
||||
throw new \InvalidArgumentException("Rule '" . $rule . "' has not been registered with " . __CLASS__ . "::addRule().");
|
||||
throw new \InvalidArgumentException(
|
||||
"Rule '" . $rule . "' has not been registered with " . get_called_class() . "::addRule()."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure rule has an accompanying message
|
||||
$msgs = $this->getRuleMessages();
|
||||
$message = isset($msgs[$rule]) ? $msgs[$rule] : self::ERROR_DEFAULT;
|
||||
$messages = $this->getRuleMessages();
|
||||
$message = isset($messages[$rule]) ? $messages[$rule] : self::ERROR_DEFAULT;
|
||||
|
||||
// Ensure message contains field label
|
||||
if (function_exists('mb_strpos')) {
|
||||
@@ -1190,8 +1402,8 @@ class Validator
|
||||
|
||||
$this->_validations[] = array(
|
||||
'rule' => $rule,
|
||||
'fields' => (array) $fields,
|
||||
'params' => (array) $params,
|
||||
'fields' => (array)$fields,
|
||||
'params' => (array)$params,
|
||||
'message' => $message
|
||||
);
|
||||
|
||||
@@ -1202,8 +1414,7 @@ class Validator
|
||||
* Add label to rule
|
||||
*
|
||||
* @param string $value
|
||||
* @internal param array $labels
|
||||
* @return $this
|
||||
* @return Validator
|
||||
*/
|
||||
public function label($value)
|
||||
{
|
||||
@@ -1217,7 +1428,7 @@ class Validator
|
||||
* Add labels to rules
|
||||
*
|
||||
* @param array $labels
|
||||
* @return $this
|
||||
* @return Validator
|
||||
*/
|
||||
public function labels($labels = array())
|
||||
{
|
||||
@@ -1228,29 +1439,29 @@ class Validator
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $msg
|
||||
* @param string $message
|
||||
* @param array $params
|
||||
* @return array
|
||||
*/
|
||||
protected function checkAndSetLabel($field, $msg, $params)
|
||||
protected function checkAndSetLabel($field, $message, $params)
|
||||
{
|
||||
if (isset($this->_labels[$field])) {
|
||||
$msg = str_replace('{field}', $this->_labels[$field], $msg);
|
||||
$message = str_replace('{field}', $this->_labels[$field], $message);
|
||||
|
||||
if (is_array($params)) {
|
||||
$i = 1;
|
||||
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;
|
||||
$msg = str_replace($tag, $label, $msg);
|
||||
$message = str_replace($tag, $label, $message);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$msg = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $msg);
|
||||
$message = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $message);
|
||||
}
|
||||
|
||||
return $msg;
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1263,8 +1474,8 @@ class Validator
|
||||
foreach ($rules as $ruleType => $params) {
|
||||
if (is_array($params)) {
|
||||
foreach ($params as $innerParams) {
|
||||
if (! is_array($innerParams)){
|
||||
$innerParams = (array) $innerParams;
|
||||
if (!is_array($innerParams)) {
|
||||
$innerParams = (array)$innerParams;
|
||||
}
|
||||
array_unshift($innerParams, $ruleType);
|
||||
call_user_func_array(array($this, 'rule'), $innerParams);
|
||||
@@ -1280,7 +1491,7 @@ class Validator
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $fields
|
||||
* @return \Valitron\Validator
|
||||
* @return Validator
|
||||
*/
|
||||
public function withData($data, $fields = array())
|
||||
{
|
||||
@@ -1293,32 +1504,33 @@ class Validator
|
||||
/**
|
||||
* Convenience method to add validation rule(s) by field
|
||||
*
|
||||
* @param string field_name
|
||||
* @param array $rules
|
||||
* @param string $field
|
||||
* @param array $rules
|
||||
*/
|
||||
public function mapFieldRules($field_name, $rules){
|
||||
public function mapFieldRules($field, $rules)
|
||||
{
|
||||
$me = $this;
|
||||
|
||||
array_map(function($rule) use($field_name, $me){
|
||||
array_map(function ($rule) use ($field, $me) {
|
||||
|
||||
//rule must be an array
|
||||
$rule = (array)$rule;
|
||||
|
||||
//First element is the name of the rule
|
||||
$rule_name = array_shift($rule);
|
||||
$ruleName = array_shift($rule);
|
||||
|
||||
//find a custom message, if any
|
||||
$message = null;
|
||||
if (isset($rule['message'])){
|
||||
if (isset($rule['message'])) {
|
||||
$message = $rule['message'];
|
||||
unset($rule['message']);
|
||||
}
|
||||
//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));
|
||||
if (! empty($message)){
|
||||
$added = call_user_func_array(array($me, 'rule'), array_merge(array($ruleName, $field), $rule));
|
||||
if (!empty($message)) {
|
||||
$added->message($message);
|
||||
}
|
||||
}, (array) $rules);
|
||||
}, (array)$rules);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1326,10 +1538,11 @@ class Validator
|
||||
*
|
||||
* @param array $rules
|
||||
*/
|
||||
public function mapFieldsRules($rules){
|
||||
public function mapFieldsRules($rules)
|
||||
{
|
||||
$me = $this;
|
||||
array_map(function($field_name) use($rules, $me){
|
||||
$me->mapFieldRules($field_name, $rules[$field_name]);
|
||||
array_map(function ($field) use ($rules, $me) {
|
||||
$me->mapFieldRules($field, $rules[$field]);
|
||||
}, array_keys($rules));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user