From 6f366d3010c1b54ecaa1e9f4918e74dbbbb9bab8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 16 Oct 2011 14:03:09 -0500 Subject: [PATCH] continuing to refactor the validator. --- laravel/validation/validator.php | 45 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/laravel/validation/validator.php b/laravel/validation/validator.php index 3b308ed9..8f5f353e 100644 --- a/laravel/validation/validator.php +++ b/laravel/validation/validator.php @@ -139,10 +139,7 @@ public function valid() foreach ($this->rules as $attribute => $rules) { - foreach ($rules as $rule) - { - $this->check($attribute, $rule); - } + foreach ($rules as $rule) $this->check($attribute, $rule); } return count($this->errors->messages) == 0; @@ -159,29 +156,35 @@ protected function check($attribute, $rule) { list($rule, $parameters) = $this->parse($rule); - if ( ! method_exists($this, $validator = 'validate_'.$rule) and ! isset(static::$validators[$rule])) - { - throw new \Exception("Validation rule [$rule] doesn't exist."); - } + // Verify that the attribute and rule combination is actually validatable before + // attempting to call the validation rule. Unless the rule implicitly requires + // the attribute to exist, we will not call any rules for attributes that are + // not in the validator's attribute array. + if ( ! $this->validatable($rule, $attribute, $value = Arr::get($this->attributes, $attribute))) return; - // 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 = Arr::get($this->attributes, $attribute); - - // 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, $value) and ! in_array($rule, array('required', 'accepted'))) - { - return; - } - - if ( ! $this->$validator($attribute, $value, $parameters, $this)) + if ( ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this)) { $this->error($attribute, $rule, $parameters); } } + /** + * Determine if an attribute is validatable. + * + * To be considered validatable, the attribute must either exist, or the rule being + * checked must implicitly validate "required", such as the "required" rule or the + * "accepted" rule. No other rules have implicit "required" validation. + * + * @param string $rule + * @param string $attribute + * @param mixed $value + * @return bool + */ + protected function validatable($rule, $attribute, $value) + { + return ($this->validate_required($attribute, $value) or in_array($rule, array('required', 'accepted'))); + } + /** * Add an error message to the validator's collection of messages. *