Minor refactoring of the Validator. Mainly code formatting and naming changes.

This commit is contained in:
Taylor Otwell 2011-11-29 13:40:33 -06:00
parent ed092bf6cd
commit 9e46cadab3
1 changed files with 19 additions and 8 deletions

View File

@ -38,7 +38,7 @@ class Validator {
*
* @var Database\Connection
*/
protected $connection;
protected $db;
/**
* The language that should be used when retrieving error messages.
@ -160,9 +160,9 @@ protected function check($attribute, $rule)
$value = Arr::get($this->attributes, $attribute);
if ( ! $this->validatable($rule, $attribute, $value)) return;
$validatable = $this->validatable($rule, $attribute, $value);
if ( ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
if ($validatable and ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
{
$this->error($attribute, $rule, $parameters);
}
@ -182,7 +182,18 @@ protected function check($attribute, $rule)
*/
protected function validatable($rule, $attribute, $value)
{
return ($this->validate_required($attribute, $value) or in_array($rule, array('required', 'accepted')));
return $this->validate_required($attribute, $value) or $this->implicit($rule);
}
/**
* Determine if a given rule implies that the attribute is required.
*
* @param string $rule
* @return bool
*/
protected function implicit($rule)
{
return $rule == 'required' or $rule == 'accepted';
}
/**
@ -401,9 +412,9 @@ protected function validate_unique($attribute, $value, $parameters)
{
if ( ! isset($parameters[1])) $parameters[1] = $attribute;
if (is_null($this->connection)) $this->connection = DB::connection();
if (is_null($this->db)) $this->db = DB::connection();
return $this->connection->table($parameters[0])->where($parameters[1], '=', $value)->count() == 0;
return $this->db->table($parameters[0])->where($parameters[1], '=', $value)->count() == 0;
}
/**
@ -678,7 +689,7 @@ public function speaks($language)
*/
public function connection(\Laravel\Database\Connection $connection)
{
$this->connection = $connection;
$this->db = $connection;
return $this;
}
@ -697,4 +708,4 @@ public function __call($method, $parameters)
throw new \BadMethodCallException("Call to undefined method [$method] on Validator instance.");
}
}
}