Commit d8d0ddfa authored by Taylor Otwell's avatar Taylor Otwell

Refactoring the validator class.

parent c145e6cb
...@@ -60,8 +60,8 @@ class Validator { ...@@ -60,8 +60,8 @@ class Validator {
} }
$this->attributes = $attributes; $this->attributes = $attributes;
$this->rules = $rules;
$this->messages = $messages; $this->messages = $messages;
$this->rules = $rules;
} }
/** /**
...@@ -125,10 +125,7 @@ class Validator { ...@@ -125,10 +125,7 @@ class Validator {
// No validation will be run for attributes that do not exist unless the rule being validated // 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. // is "required" or "accepted". No other rules have implicit "required" checks.
if ( ! static::validate_required($attribute) and ! in_array($rule, array('required', 'accepted'))) if ( ! static::validate_required($attribute) and ! in_array($rule, array('required', 'accepted'))) return;
{
return;
}
if ( ! $this->$validator($attribute, $parameters)) if ( ! $this->$validator($attribute, $parameters))
{ {
...@@ -144,15 +141,9 @@ class Validator { ...@@ -144,15 +141,9 @@ class Validator {
*/ */
protected function validate_required($attribute) protected function validate_required($attribute)
{ {
if ( ! array_key_exists($attribute, $this->attributes)) if ( ! array_key_exists($attribute, $this->attributes)) return false;
{
return false;
}
if (is_string($this->attributes[$attribute]) and trim($this->attributes[$attribute]) === '') if (is_string($this->attributes[$attribute]) and trim($this->attributes[$attribute]) === '') return false;
{
return false;
}
return true; return true;
} }
...@@ -259,12 +250,9 @@ class Validator { ...@@ -259,12 +250,9 @@ class Validator {
*/ */
protected function get_size($attribute) protected function get_size($attribute)
{ {
if (is_numeric($this->attributes[$attribute])) if (is_numeric($this->attributes[$attribute])) return $this->attributes[$attribute];
{
return $this->attributes[$attribute];
}
return (array_key_exists($attribute, $_FILES)) ? $this->attributes[$attribute]['size'] / 1000 : Str::length(trim($this->attributes[$attribute])); return (array_key_exists($attribute, $_FILES)) ? $this->attributes[$attribute]['size'] / 1024 : Str::length(trim($this->attributes[$attribute]));
} }
/** /**
...@@ -294,16 +282,15 @@ class Validator { ...@@ -294,16 +282,15 @@ class Validator {
/** /**
* Validate the uniqueness of an attribute value on a given database table. * Validate the uniqueness of an attribute value on a given database table.
* *
* If a database column is not specified, the attribute name will be used.
*
* @param string $attribute * @param string $attribute
* @param array $parameters * @param array $parameters
* @return bool * @return bool
*/ */
protected function validate_unique($attribute, $parameters) protected function validate_unique($attribute, $parameters)
{ {
if ( ! isset($parameters[1])) if ( ! isset($parameters[1])) $parameters[1] = $attribute;
{
$parameters[1] = $attribute;
}
return DB\Manager::connection()->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0; return DB\Manager::connection()->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0;
} }
...@@ -398,10 +385,7 @@ class Validator { ...@@ -398,10 +385,7 @@ class Validator {
{ {
foreach ($parameters as $extension) foreach ($parameters as $extension)
{ {
if (File::is($extension, $this->attributes[$attribute]['tmp_name'])) if (File::is($extension, $this->attributes[$attribute]['tmp_name'])) return true;
{
return true;
}
} }
return false; return false;
...@@ -435,7 +419,7 @@ class Validator { ...@@ -435,7 +419,7 @@ class Validator {
$message = Lang::line('validation.'.$rule)->get($this->language); $message = Lang::line('validation.'.$rule)->get($this->language);
// For "size" rules that are validating strings or files, we need to adjust // For "size" rules that are validating strings or files, we need to adjust
// the default error message appropriately. // the default error message for the appropriate type.
if (in_array($rule, $this->size_rules) and ! is_numeric($this->attributes[$attribute])) if (in_array($rule, $this->size_rules) and ! is_numeric($this->attributes[$attribute]))
{ {
return (array_key_exists($attribute, $_FILES)) return (array_key_exists($attribute, $_FILES))
...@@ -458,7 +442,7 @@ class Validator { ...@@ -458,7 +442,7 @@ class Validator {
*/ */
protected function format_message($message, $attribute, $rule, $parameters) protected function format_message($message, $attribute, $rule, $parameters)
{ {
$display = Lang::line('attributes.'.$attribute)->get($this->language, function() use ($attribute) { return str_replace('_', ' ', $attribute); }); $display = Lang::line('attributes.'.$attribute)->get($this->language, str_replace('_', ' ', $attribute));
$message = str_replace(':attribute', $display, $message); $message = str_replace(':attribute', $display, $message);
...@@ -466,7 +450,7 @@ class Validator { ...@@ -466,7 +450,7 @@ class Validator {
{ {
$max = ($rule == 'between') ? $parameters[1] : $parameters[0]; $max = ($rule == 'between') ? $parameters[1] : $parameters[0];
$message = str_replace(':size', $parameters[0], str_replace(':min', $parameters[0], str_replace(':max', $max, $message))); $message = str_replace(array(':size', ':min', ':max'), array($parameters[0], $parameters[0], $max), $message);
} }
elseif (in_array($rule, array('in', 'not_in', 'mimes'))) elseif (in_array($rule, array('in', 'not_in', 'mimes')))
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment