mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-31 07:01:54 +00:00
Array contains and unique validators added, code style fixes
This commit is contained in:
parent
20a9c2d604
commit
d9a466b833
@ -132,6 +132,8 @@ V::lang('ar');
|
|||||||
* `dateBefore` - Field is a valid date and is before the given date
|
* `dateBefore` - Field is a valid date and is before the given date
|
||||||
* `dateAfter` - Field is a valid date and is after the given date
|
* `dateAfter` - Field is a valid date and is after the given date
|
||||||
* `contains` - Field is a string and contains the given string
|
* `contains` - Field is a string and contains the given string
|
||||||
|
* `arrayContains` - Field is an array and contains the given array
|
||||||
|
* `unique` - Field is an array and contains unique values
|
||||||
* `creditCard` - Field is a valid credit card number
|
* `creditCard` - Field is a valid credit card number
|
||||||
* `instanceOf` - Field contains an instance of the given class
|
* `instanceOf` - Field contains an instance of the given class
|
||||||
* `optional` - Value does not need to be included in data array. If it is however, it must pass validation.
|
* `optional` - Value does not need to be included in data array. If it is however, it must pass validation.
|
||||||
|
|||||||
@ -100,7 +100,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 {
|
||||||
@ -146,9 +146,9 @@ class Validator
|
|||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateRequired($field, $value, array $params= array(), array $fields = array())
|
protected function validateRequired($field, $value, array $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];
|
||||||
}
|
}
|
||||||
@ -168,7 +168,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateEquals($field, $value, array $params)
|
protected function validateEquals($field, $value, array $params)
|
||||||
@ -184,7 +183,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateDifferent($field, $value, array $params)
|
protected function validateDifferent($field, $value, array $params)
|
||||||
@ -244,7 +242,7 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
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])+$/i', $value);
|
return preg_match('/^-?([0-9])+$/i', $value);
|
||||||
}
|
}
|
||||||
@ -258,7 +256,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateLength($field, $value, $params)
|
protected function validateLength($field, $value, $params)
|
||||||
@ -278,7 +275,7 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return boolean
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateLengthBetween($field, $value, $params)
|
protected function validateLengthBetween($field, $value, $params)
|
||||||
{
|
{
|
||||||
@ -294,7 +291,7 @@ class Validator
|
|||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateLengthMin($field, $value, $params)
|
protected function validateLengthMin($field, $value, $params)
|
||||||
{
|
{
|
||||||
@ -310,7 +307,7 @@ class Validator
|
|||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateLengthMax($field, $value, $params)
|
protected function validateLengthMax($field, $value, $params)
|
||||||
{
|
{
|
||||||
@ -342,7 +339,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateMin($field, $value, $params)
|
protected function validateMin($field, $value, $params)
|
||||||
@ -362,7 +358,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateMax($field, $value, $params)
|
protected function validateMax($field, $value, $params)
|
||||||
@ -405,7 +400,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateIn($field, $value, $params)
|
protected function validateIn($field, $value, $params)
|
||||||
@ -429,7 +423,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateNotIn($field, $value, $params)
|
protected function validateNotIn($field, $value, $params)
|
||||||
@ -441,7 +434,7 @@ 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 string $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@ -456,10 +449,9 @@ class Validator
|
|||||||
|
|
||||||
$strict = true;
|
$strict = true;
|
||||||
if (isset($params[1])) {
|
if (isset($params[1])) {
|
||||||
$strict = (bool) $params[1];
|
$strict = (bool)$params[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
$isContains = false;
|
|
||||||
if ($strict) {
|
if ($strict) {
|
||||||
if (function_exists('mb_strpos')) {
|
if (function_exists('mb_strpos')) {
|
||||||
$isContains = mb_strpos($value, $params[0]) !== false;
|
$isContains = mb_strpos($value, $params[0]) !== false;
|
||||||
@ -476,6 +468,43 @@ class Validator
|
|||||||
return $isContains;
|
return $isContains;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate that all field values contains a given array
|
||||||
|
*
|
||||||
|
* @param string $field
|
||||||
|
* @param array $value
|
||||||
|
* @param array $params
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function validateArrayContains($field, $value, $params)
|
||||||
|
{
|
||||||
|
if (!isset($params[0])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!is_array($value) || !is_array($params[0])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$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 validateUnique($field, $value)
|
||||||
|
{
|
||||||
|
if (!is_array($value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value === array_unique($value, SORT_REGULAR);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate that a field is a valid IP address
|
* Validate that a field is a valid IP address
|
||||||
*
|
*
|
||||||
@ -571,7 +600,7 @@ class Validator
|
|||||||
*/
|
*/
|
||||||
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);
|
||||||
@ -615,7 +644,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateDateFormat($field, $value, $params)
|
protected function validateDateFormat($field, $value, $params)
|
||||||
@ -631,7 +659,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateDateBefore($field, $value, $params)
|
protected function validateDateBefore($field, $value, $params)
|
||||||
@ -648,7 +675,6 @@ class Validator
|
|||||||
* @param string $field
|
* @param string $field
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @internal param array $fields
|
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
protected function validateDateAfter($field, $value, $params)
|
protected function validateDateAfter($field, $value, $params)
|
||||||
@ -802,7 +828,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;
|
||||||
}
|
}
|
||||||
@ -836,12 +863,12 @@ class Validator
|
|||||||
* Add an error to error messages array
|
* Add an error to error messages array
|
||||||
*
|
*
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param string $msg
|
* @param string $message
|
||||||
* @param array $params
|
* @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();
|
$values = array();
|
||||||
// Printed values need to be in string format
|
// Printed values need to be in string format
|
||||||
@ -865,18 +892,18 @@ class Validator
|
|||||||
$values[] = $param;
|
$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
|
* Specify validation message to use for error for the last validation rule
|
||||||
*
|
*
|
||||||
* @param string $msg
|
* @param string $message
|
||||||
* @return $this
|
* @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;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -941,7 +968,7 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Run validations and return boolean result
|
* Run validations and return boolean result
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validate()
|
public function validate()
|
||||||
{
|
{
|
||||||
@ -1011,9 +1038,8 @@ class Validator
|
|||||||
*
|
*
|
||||||
* @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 bool
|
||||||
*/
|
*/
|
||||||
|
|
||||||
protected function hasRule($name, $field)
|
protected function hasRule($name, $field)
|
||||||
{
|
{
|
||||||
foreach ($this->_validations as $validation) {
|
foreach ($this->_validations as $validation) {
|
||||||
@ -1030,11 +1056,12 @@ class Validator
|
|||||||
protected static function assertRuleCallback($callback)
|
protected static function assertRuleCallback($callback)
|
||||||
{
|
{
|
||||||
if (!is_callable($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
|
* Adds a new validation rule callback that is tied to the current
|
||||||
* instance only.
|
* instance only.
|
||||||
@ -1073,18 +1100,20 @@ class Validator
|
|||||||
static::$_ruleMessages[$name] = $message;
|
static::$_ruleMessages[$name] = $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $fields
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1110,7 +1139,7 @@ class Validator
|
|||||||
*
|
*
|
||||||
* @param string|callback $rule
|
* @param string|callback $rule
|
||||||
* @param array|string $fields
|
* @param array|string $fields
|
||||||
* @return $this
|
* @return Validator
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function rule($rule, $fields)
|
public function rule($rule, $fields)
|
||||||
@ -1118,12 +1147,10 @@ class Validator
|
|||||||
// Get any other arguments passed to function
|
// Get any other arguments passed to function
|
||||||
$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;
|
$message = isset($params[0]) ? $params[0] : null;
|
||||||
$this->addInstanceRule($name, $rule, $msg);
|
$this->addInstanceRule($name, $rule, $message);
|
||||||
$rule = $name;
|
$rule = $name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1131,13 +1158,15 @@ class Validator
|
|||||||
if (!isset($errors[$rule])) {
|
if (!isset($errors[$rule])) {
|
||||||
$ruleMethod = 'validate' . ucfirst($rule);
|
$ruleMethod = 'validate' . ucfirst($rule);
|
||||||
if (!method_exists($this, $ruleMethod)) {
|
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 " . __CLASS__ . "::addRule()."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure rule has an accompanying message
|
// Ensure rule has an accompanying message
|
||||||
$msgs = $this->getRuleMessages();
|
$messages = $this->getRuleMessages();
|
||||||
$message = isset($msgs[$rule]) ? $msgs[$rule] : self::ERROR_DEFAULT;
|
$message = isset($messages[$rule]) ? $messages[$rule] : self::ERROR_DEFAULT;
|
||||||
|
|
||||||
// Ensure message contains field label
|
// Ensure message contains field label
|
||||||
if (function_exists('mb_strpos')) {
|
if (function_exists('mb_strpos')) {
|
||||||
@ -1151,8 +1180,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
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -1161,8 +1190,7 @@ class Validator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @internal param array $labels
|
* @return Validator
|
||||||
* @return $this
|
|
||||||
*/
|
*/
|
||||||
public function label($value)
|
public function label($value)
|
||||||
{
|
{
|
||||||
@ -1174,7 +1202,7 @@ class Validator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $labels
|
* @param array $labels
|
||||||
* @return $this
|
* @return Validator
|
||||||
*/
|
*/
|
||||||
public function labels($labels = array())
|
public function labels($labels = array())
|
||||||
{
|
{
|
||||||
@ -1185,29 +1213,29 @@ class Validator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param string $msg
|
* @param string $message
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function checkAndSetLabel($field, $msg, $params)
|
protected function checkAndSetLabel($field, $message, $params)
|
||||||
{
|
{
|
||||||
if (isset($this->_labels[$field])) {
|
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)) {
|
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);
|
$message = str_replace($tag, $label, $message);
|
||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$msg = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $msg);
|
$message = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $msg;
|
return $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1220,8 +1248,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);
|
||||||
@ -1237,7 +1265,7 @@ class Validator
|
|||||||
*
|
*
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @param array $fields
|
* @param array $fields
|
||||||
* @return \Valitron\Validator
|
* @return Validator
|
||||||
*/
|
*/
|
||||||
public function withData($data, $fields = array())
|
public function withData($data, $fields = array())
|
||||||
{
|
{
|
||||||
@ -1250,29 +1278,30 @@ class Validator
|
|||||||
/**
|
/**
|
||||||
* Convenience method to add validation rule(s) by field
|
* Convenience method to add validation rule(s) by field
|
||||||
*
|
*
|
||||||
* @param string field_name
|
* @param string $field
|
||||||
* @param array $rules
|
* @param array $rules
|
||||||
*/
|
*/
|
||||||
public function mapFieldRules($field_name, $rules){
|
public function mapFieldRules($field, $rules)
|
||||||
|
{
|
||||||
$me = $this;
|
$me = $this;
|
||||||
|
|
||||||
array_map(function($rule) use($field_name, $me){
|
array_map(function ($rule) use ($field, $me) {
|
||||||
|
|
||||||
//rule must be an array
|
//rule must be an array
|
||||||
$rule = (array)$rule;
|
$rule = (array)$rule;
|
||||||
|
|
||||||
//First element is the name of the rule
|
//First element is the name of the rule
|
||||||
$rule_name = array_shift($rule);
|
$ruleName = array_shift($rule);
|
||||||
|
|
||||||
//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($ruleName, $field), $rule));
|
||||||
if (! empty($message)){
|
if (!empty($message)) {
|
||||||
$added->message($message);
|
$added->message($message);
|
||||||
}
|
}
|
||||||
}, (array) $rules);
|
}, (array) $rules);
|
||||||
@ -1283,10 +1312,11 @@ 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) use ($rules, $me) {
|
||||||
$me->mapFieldRules($field_name, $rules[$field_name]);
|
$me->mapFieldRules($field, $rules[$field]);
|
||||||
}, array_keys($rules));
|
}, array_keys($rules));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,8 +7,9 @@ class StaticVsInstanceTest extends BaseTestCase
|
|||||||
{
|
{
|
||||||
Validator::lang('ar');
|
Validator::lang('ar');
|
||||||
new Validator(array(), array(), 'en');
|
new Validator(array(), array(), 'en');
|
||||||
$this->assertEquals('ar', Validator::lang(),
|
$this->assertEquals(
|
||||||
'instance defined lang should not replace static global lang');
|
'ar', Validator::lang(), 'instance defined lang should not replace static global lang'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Valitron\Validator;
|
use Valitron\Validator;
|
||||||
|
|
||||||
function callbackTestFunction($item, $value)
|
function callbackTestFunction($item, $value)
|
||||||
@ -8,14 +9,15 @@ function callbackTestFunction($item, $value)
|
|||||||
|
|
||||||
class ValidateAddInstanceRuleTest extends BaseTestCase
|
class ValidateAddInstanceRuleTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param Validator $v
|
||||||
|
*/
|
||||||
protected function assertValid($v)
|
protected function assertValid($v)
|
||||||
{
|
{
|
||||||
$msg = "\tErrors:\n";
|
$msg = "\tErrors:\n";
|
||||||
$status = $v->validate();
|
$status = $v->validate();
|
||||||
foreach ($v->errors() as $label => $messages)
|
foreach ($v->errors() as $label => $messages) {
|
||||||
{
|
foreach ($messages as $theMessage) {
|
||||||
foreach ($messages as $theMessage)
|
|
||||||
{
|
|
||||||
$msg .= "\n\t{$label}: {$theMessage}";
|
$msg .= "\n\t{$label}: {$theMessage}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -30,13 +32,11 @@ class ValidateAddInstanceRuleTest extends BaseTestCase
|
|||||||
"fuzz" => "bazz",
|
"fuzz" => "bazz",
|
||||||
));
|
));
|
||||||
|
|
||||||
$v->addInstanceRule("fooRule", function($field, $value)
|
$v->addInstanceRule("fooRule", function ($field, $value) {
|
||||||
{
|
|
||||||
return $field !== "foo" || $value !== "barz";
|
return $field !== "foo" || $value !== "barz";
|
||||||
});
|
});
|
||||||
|
|
||||||
Validator::addRule("fuzzerRule", function($field, $value)
|
Validator::addRule("fuzzerRule", function ($field, $value) {
|
||||||
{
|
|
||||||
return $field !== "fuzz" || $value === "bazz";
|
return $field !== "fuzz" || $value === "bazz";
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -50,8 +50,7 @@ class ValidateAddInstanceRuleTest extends BaseTestCase
|
|||||||
public function testAddInstanceRuleFail()
|
public function testAddInstanceRuleFail()
|
||||||
{
|
{
|
||||||
$v = new Validator(array("foo" => "bar"));
|
$v = new Validator(array("foo" => "bar"));
|
||||||
$v->addInstanceRule("fooRule", function($field)
|
$v->addInstanceRule("fooRule", function ($field) {
|
||||||
{
|
|
||||||
return $field === "for";
|
return $field === "for";
|
||||||
});
|
});
|
||||||
$v->rule("fooRule", "foo");
|
$v->rule("fooRule", "foo");
|
||||||
@ -61,7 +60,7 @@ class ValidateAddInstanceRuleTest extends BaseTestCase
|
|||||||
public function testAddAddRuleWithCallback()
|
public function testAddAddRuleWithCallback()
|
||||||
{
|
{
|
||||||
$v = new Validator(array("foo" => "bar"));
|
$v = new Validator(array("foo" => "bar"));
|
||||||
$v->rule(function($field, $value) {
|
$v->rule(function ($field, $value) {
|
||||||
return $field === "foo" && $value === "bar";
|
return $field === "foo" && $value === "bar";
|
||||||
}, "foo");
|
}, "foo");
|
||||||
|
|
||||||
@ -71,7 +70,7 @@ class ValidateAddInstanceRuleTest extends BaseTestCase
|
|||||||
public function testAddAddRuleWithCallbackFail()
|
public function testAddAddRuleWithCallbackFail()
|
||||||
{
|
{
|
||||||
$v = new Validator(array("foo" => "baz"));
|
$v = new Validator(array("foo" => "baz"));
|
||||||
$v->rule(function($field, $value) {
|
$v->rule(function ($field, $value) {
|
||||||
return $field === "foo" && $value === "bar";
|
return $field === "foo" && $value === "bar";
|
||||||
}, "foo");
|
}, "foo");
|
||||||
|
|
||||||
@ -81,7 +80,7 @@ class ValidateAddInstanceRuleTest extends BaseTestCase
|
|||||||
public function testAddAddRuleWithCallbackFailMessage()
|
public function testAddAddRuleWithCallbackFailMessage()
|
||||||
{
|
{
|
||||||
$v = new Validator(array("foo" => "baz"));
|
$v = new Validator(array("foo" => "baz"));
|
||||||
$v->rule(function($field, $value) {
|
$v->rule(function ($field, $value) {
|
||||||
return $field === "foo" && $value === "bar";
|
return $field === "foo" && $value === "bar";
|
||||||
}, "foo", "test error message");
|
}, "foo", "test error message");
|
||||||
|
|
||||||
@ -113,7 +112,8 @@ class ValidateAddInstanceRuleTest extends BaseTestCase
|
|||||||
$this->assertEquals("foo_bar_rule", $v->getUniqueRuleName($args));
|
$this->assertEquals("foo_bar_rule", $v->getUniqueRuleName($args));
|
||||||
$this->assertEquals("foo_rule", $v->getUniqueRuleName("foo"));
|
$this->assertEquals("foo_rule", $v->getUniqueRuleName("foo"));
|
||||||
|
|
||||||
$v->addInstanceRule("foo_rule", function() {});
|
$v->addInstanceRule("foo_rule", function () {
|
||||||
|
});
|
||||||
$u = $v->getUniqueRuleName("foo");
|
$u = $v->getUniqueRuleName("foo");
|
||||||
$this->assertRegExp("/^foo_rule_[0-9]{1,5}$/", $u);
|
$this->assertRegExp("/^foo_rule_[0-9]{1,5}$/", $u);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -768,14 +768,14 @@ class ValidateTest extends BaseTestCase
|
|||||||
$this->assertTrue($v->validate());
|
$this->assertTrue($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testContainsNotFound()
|
public function testContainsInvalid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('test_string' => 'this is a test'));
|
$v = new Validator(array('test_string' => 'this is a test'));
|
||||||
$v->rule('contains', 'test_string', 'foobar');
|
$v->rule('contains', 'test_string', 'foobar');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testContainsStrictNotFound()
|
public function testContainsStrictInvalid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('test_string' => 'this is a Test'));
|
$v = new Validator(array('test_string' => 'this is a Test'));
|
||||||
$v->rule('contains', 'test_string', 'test');
|
$v->rule('contains', 'test_string', 'test');
|
||||||
@ -783,9 +783,93 @@ class ValidateTest extends BaseTestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testContainsInvalidValue()
|
public function testContainsInvalidValue()
|
||||||
|
{
|
||||||
|
$v = new Validator(array('test_string' => false));
|
||||||
|
$v->rule('contains', 'test_string', 'foobar');
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testContainsInvalidRule()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('test_string' => 'this is a test'));
|
$v = new Validator(array('test_string' => 'this is a test'));
|
||||||
$v->rule('contains', 'test_string', array('test'));
|
$v->rule('contains', 'test_string', null);
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayContainsValid()
|
||||||
|
{
|
||||||
|
// numeric values
|
||||||
|
$v = new Validator(array('test_field' => array(81, 3, 15)));
|
||||||
|
$v->rule('arrayContains', 'test_field', array(45, 15, 3, 7, 28, 81));
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
|
||||||
|
// string values
|
||||||
|
$v = new Validator(array('test_field' => array('white', 'green', 'blue')));
|
||||||
|
$v->rule('arrayContains', 'test_field', array('green', 'orange', 'blue', 'yellow', 'white', 'brown'));
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
|
||||||
|
// mixed values
|
||||||
|
$v = new Validator(array('test_field' => array(81, false, 'orange')));
|
||||||
|
$v->rule('arrayContains', 'test_field', array(45, 'green', true, 'orange', null, 81, false));
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayContainsInvalid()
|
||||||
|
{
|
||||||
|
$v = new Validator(array('test_field' => array(81, false, 'orange')));
|
||||||
|
$v->rule('arrayContains', 'test_field', array(45, 'green', true, 'orange', null, false, 7));
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayContainsInvalidValue()
|
||||||
|
{
|
||||||
|
$v = new Validator(array('test_field' => 'black 45'));
|
||||||
|
$v->rule('arrayContains', 'test_field', array('black', 45));
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testArrayContainsInvalidRule()
|
||||||
|
{
|
||||||
|
// rule value has invalid type
|
||||||
|
$v = new Validator(array('test_field' => array('black', 45)));
|
||||||
|
$v->rule('arrayContains', 'test_field', 'black 45');
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
|
||||||
|
// rule value not specified
|
||||||
|
$v = new Validator(array('test_field' => array('black', 45)));
|
||||||
|
$v->rule('arrayContains', 'test_field');
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUniqueValid()
|
||||||
|
{
|
||||||
|
// numeric values
|
||||||
|
$v = new Validator(array('test_field' => array(81, 3, 15)));
|
||||||
|
$v->rule('unique', 'test_field');
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
|
||||||
|
// string values
|
||||||
|
$v = new Validator(array('test_field' => array('white', 'green', 'blue')));
|
||||||
|
$v->rule('unique', 'test_field');
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
|
||||||
|
// mixed values
|
||||||
|
$v = new Validator(array('test_field' => array(81, false, 'orange')));
|
||||||
|
$v->rule('unique', 'test_field');
|
||||||
|
$this->assertTrue($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUniqueInvalid()
|
||||||
|
{
|
||||||
|
$v = new Validator(array('test_field' => array(81, false, 'orange', false)));
|
||||||
|
$v->rule('unique', 'test_field');
|
||||||
|
$this->assertFalse($v->validate());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testUniqueInvalidValue()
|
||||||
|
{
|
||||||
|
$v = new Validator(array('test_field' => 'lorem ipsum'));
|
||||||
|
$v->rule('unique', 'test_field');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1244,35 +1328,37 @@ 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'));
|
$v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text'));
|
||||||
$this->assertFalse($v2->validate());
|
$this->assertFalse($v2->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