refactoring routing and comments.
This commit is contained in:
@@ -10,11 +10,25 @@ use Laravel\Database\Manager as DB;
|
||||
class Validator {
|
||||
|
||||
/**
|
||||
* The registered custom validators.
|
||||
* The database connection that should be used by the validator.
|
||||
*
|
||||
* @var Database\Connection
|
||||
*/
|
||||
public $connection;
|
||||
|
||||
/**
|
||||
* The array being validated.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $validators = array();
|
||||
public $attributes;
|
||||
|
||||
/**
|
||||
* The post-validation error messages.
|
||||
*
|
||||
* @var Messages
|
||||
*/
|
||||
public $errors;
|
||||
|
||||
/**
|
||||
* The validation rules.
|
||||
@@ -37,6 +51,13 @@ class Validator {
|
||||
*/
|
||||
protected $language;
|
||||
|
||||
/**
|
||||
* The registered custom validators.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $validators = array();
|
||||
|
||||
/**
|
||||
* The size related validation rules.
|
||||
*
|
||||
@@ -51,27 +72,6 @@ class Validator {
|
||||
*/
|
||||
protected $numeric_rules = array('numeric', 'integer');
|
||||
|
||||
/**
|
||||
* The database connection that should be used by the validator.
|
||||
*
|
||||
* @var Database\Connection
|
||||
*/
|
||||
public $connection;
|
||||
|
||||
/**
|
||||
* The array being validated.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $attributes;
|
||||
|
||||
/**
|
||||
* The post-validation error messages.
|
||||
*
|
||||
* @var Messages
|
||||
*/
|
||||
public $errors;
|
||||
|
||||
/**
|
||||
* Create a new validator instance.
|
||||
*
|
||||
@@ -163,29 +163,50 @@ class Validator {
|
||||
throw new \Exception("Validation rule [$rule] doesn't exist.");
|
||||
}
|
||||
|
||||
// No validation will be run for attributes that do not exist unless the rule being validated
|
||||
// is "required" or "accepted". No other rules have implicit "required" checks.
|
||||
// Extract the actual value for the attribute. We don't want every rule
|
||||
// to worry about obtaining the value from the array of attributes.
|
||||
$value = (isset($this->attributes[$attribute])) ? $this->attributes[$attribute] : null;
|
||||
|
||||
// No validation will be run for attributes that do not exist unless the
|
||||
// rule being validated is "required" or "accepted". No other rules have
|
||||
// implicit "required" checks for validation.
|
||||
if ( ! $this->validate_required($attribute) and ! in_array($rule, array('required', 'accepted'))) return;
|
||||
|
||||
if ( ! $this->$validator($attribute, $parameters, $this))
|
||||
if ( ! $this->$validator($attribute, $value, $parameters, $this))
|
||||
{
|
||||
$message = $this->format_message($this->get_message($attribute, $rule), $attribute, $rule, $parameters);
|
||||
|
||||
$this->errors->add($attribute, $message, $attribute, $rule, $parameters);
|
||||
$this->error($attribute, $rule, $parameters);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an error message to the validator's collection of messages.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string $rule
|
||||
* @param array $parameters
|
||||
* @return void
|
||||
*/
|
||||
protected function error($attribute, $rule, $parameters)
|
||||
{
|
||||
$message = $this->get_message($attribute, $rule);
|
||||
|
||||
$message = $this->format_message($message, $attribute, $rule, $parameters);
|
||||
|
||||
$this->errors->add($attribute, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a required attribute exists in the attributes array.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_required($attribute)
|
||||
protected function validate_required($attribute, $value)
|
||||
{
|
||||
if ( ! array_key_exists($attribute, $this->attributes)) return false;
|
||||
if (is_null($value)) return false;
|
||||
|
||||
if (is_string($this->attributes[$attribute]) and trim($this->attributes[$attribute]) === '') return false;
|
||||
if (is_string($value) and trim($value) === '') return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -194,12 +215,11 @@ class Validator {
|
||||
* Validate that an attribute has a matching confirmation attribute.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_confirmed($attribute)
|
||||
protected function validate_confirmed($attribute, $value)
|
||||
{
|
||||
$value = $this->attributes[$attribute];
|
||||
|
||||
$confirmation = $this->attributes[$attribute.'_confirmation'];
|
||||
|
||||
return array_key_exists($attribute.'_confirmation', $this->attributes) and $value == $confirmation;
|
||||
@@ -211,12 +231,11 @@ class Validator {
|
||||
* This validation rule implies the attribute is "required".
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_accepted($attribute)
|
||||
protected function validate_accepted($attribute, $value)
|
||||
{
|
||||
$value = $this->attributes[$attribute];
|
||||
|
||||
return $this->validate_required($attribute) and ($value == 'yes' or $value == '1');
|
||||
}
|
||||
|
||||
@@ -224,32 +243,35 @@ class Validator {
|
||||
* Validate that an attribute is numeric.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_numeric($attribute)
|
||||
protected function validate_numeric($attribute, $value)
|
||||
{
|
||||
return is_numeric($this->attributes[$attribute]);
|
||||
return is_numeric($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that an attribute is an integer.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_integer($attribute)
|
||||
protected function validate_integer($attribute, $value)
|
||||
{
|
||||
return filter_var($this->attributes[$attribute], FILTER_VALIDATE_INT) !== false;
|
||||
return filter_var($value, FILTER_VALIDATE_INT) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the size of an attribute.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_size($attribute, $parameters)
|
||||
protected function validate_size($attribute, $value, $parameters)
|
||||
{
|
||||
return $this->get_size($attribute) == $parameters[0];
|
||||
}
|
||||
@@ -258,10 +280,11 @@ class Validator {
|
||||
* Validate the size of an attribute is between a set of values.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_between($attribute, $parameters)
|
||||
protected function validate_between($attribute, $value, $parameters)
|
||||
{
|
||||
return $this->get_size($attribute) >= $parameters[0] and $this->get_size($attribute) <= $parameters[1];
|
||||
}
|
||||
@@ -270,10 +293,11 @@ class Validator {
|
||||
* Validate the size of an attribute is greater than a minimum value.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_min($attribute, $parameters)
|
||||
protected function validate_min($attribute, $value, $parameters)
|
||||
{
|
||||
return $this->get_size($attribute) >= $parameters[0];
|
||||
}
|
||||
@@ -282,10 +306,11 @@ class Validator {
|
||||
* Validate the size of an attribute is less than a maximum value.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_max($attribute, $parameters)
|
||||
protected function validate_max($attribute, $value, $parameters)
|
||||
{
|
||||
return $this->get_size($attribute) <= $parameters[0];
|
||||
}
|
||||
@@ -305,31 +330,40 @@ class Validator {
|
||||
|
||||
$value = $this->attributes[$attribute];
|
||||
|
||||
return (array_key_exists($attribute, Input::file())) ? $value['size'] / 1024 : Str::length(trim($value));
|
||||
if (array_key_exists($attribute, Input::file()))
|
||||
{
|
||||
return $value['size'] / 1024;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Str::length(trim($value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate an attribute is contained within a list of values.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_in($attribute, $parameters)
|
||||
protected function validate_in($attribute, $value, $parameters)
|
||||
{
|
||||
return in_array($this->attributes[$attribute], $parameters);
|
||||
return in_array($value, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate an attribute is not contained within a list of values.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_not_in($attribute, $parameters)
|
||||
protected function validate_not_in($attribute, $value, $parameters)
|
||||
{
|
||||
return ! in_array($this->attributes[$attribute], $parameters);
|
||||
return ! in_array($value, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -338,49 +372,53 @@ class Validator {
|
||||
* If a database column is not specified, the attribute name will be used.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param array $parameters
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_unique($attribute, $parameters)
|
||||
protected function validate_unique($attribute, $value, $parameters)
|
||||
{
|
||||
if ( ! isset($parameters[1])) $parameters[1] = $attribute;
|
||||
|
||||
if (is_null($this->connection)) $this->connection = DB::connection();
|
||||
|
||||
return $this->connection->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0;
|
||||
return $this->connection->table($parameters[0])->where($parameters[1], '=', $value)->count() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate than an attribute is a valid e-mail address.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_email($attribute)
|
||||
protected function validate_email($attribute, $value)
|
||||
{
|
||||
return filter_var($this->attributes[$attribute], FILTER_VALIDATE_EMAIL) !== false;
|
||||
return filter_var($value, FILTER_VALIDATE_EMAIL) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate than an attribute is a valid URL.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_url($attribute)
|
||||
protected function validate_url($attribute, $value)
|
||||
{
|
||||
return filter_var($this->attributes[$attribute], FILTER_VALIDATE_URL) !== false;
|
||||
return filter_var($value, FILTER_VALIDATE_URL) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that an attribute is an active URL.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_active_url($attribute)
|
||||
protected function validate_active_url($attribute, $value)
|
||||
{
|
||||
$url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($this->attributes[$attribute]));
|
||||
$url = str_replace(array('http://', 'https://', 'ftp://'), '', Str::lower($value));
|
||||
|
||||
return checkdnsrr($url);
|
||||
}
|
||||
@@ -389,9 +427,10 @@ class Validator {
|
||||
* Validate the MIME type of a file is an image MIME type.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_image($attribute)
|
||||
protected function validate_image($attribute, $value)
|
||||
{
|
||||
return $this->validate_mimes($attribute, array('jpg', 'png', 'gif', 'bmp'));
|
||||
}
|
||||
@@ -400,33 +439,36 @@ class Validator {
|
||||
* Validate than an attribute contains only alphabetic characters.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_alpha($attribute)
|
||||
protected function validate_alpha($attribute, $value)
|
||||
{
|
||||
return preg_match('/^([a-z])+$/i', $this->attributes[$attribute]);
|
||||
return preg_match('/^([a-z])+$/i', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate than an attribute contains only alpha-numeric characters.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_alpha_num($attribute)
|
||||
protected function validate_alpha_num($attribute, $value)
|
||||
{
|
||||
return preg_match('/^([a-z0-9])+$/i', $this->attributes[$attribute]);
|
||||
return preg_match('/^([a-z0-9])+$/i', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate than an attribute contains only alpha-numeric characters, dashes, and underscores.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validate_alpha_dash($attribute)
|
||||
protected function validate_alpha_dash($attribute, $value)
|
||||
{
|
||||
return preg_match('/^([-a-z0-9_-])+$/i', $this->attributes[$attribute]);
|
||||
return preg_match('/^([-a-z0-9_-])+$/i', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -549,9 +591,9 @@ class Validator {
|
||||
*/
|
||||
protected function parse($rule)
|
||||
{
|
||||
// The format for specifying validation rules and parameters follows a {rule}:{parameters}
|
||||
// convention. For instance, "max:3" specifies that the value may only be 3 characters in
|
||||
// length. And "unique:users" specifies that a value must be unique on the "users" table.
|
||||
// The format for specifying validation rules and parameters follows
|
||||
// a {rule}:{parameters} convention. For instance, "max:3" specifies
|
||||
// that the value may only be 3 characters in length.
|
||||
$parameters = (($colon = strpos($rule, ':')) !== false) ? explode(',', substr($rule, $colon + 1)) : array();
|
||||
|
||||
return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
|
||||
@@ -586,9 +628,9 @@ class Validator {
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
// First we will slice the "validate_" prefix off of the validator since custom
|
||||
// validators are not registered with such a prefix. We will then call the validator
|
||||
// and pass it the parameters we received.
|
||||
// First we will slice the "validate_" prefix off of the validator
|
||||
// since custom validators are not registered with such a prefix.
|
||||
// Then, if a custom validator exists, we will call it.
|
||||
if (isset(static::$validators[$method = substr($method, 9)]))
|
||||
{
|
||||
return call_user_func_array(static::$validators[$method], $parameters);
|
||||
|
||||
Reference in New Issue
Block a user