continuing to refactor the validator.
This commit is contained in:
parent
ca784e9fc2
commit
6f366d3010
|
@ -139,10 +139,7 @@ public function valid()
|
||||||
|
|
||||||
foreach ($this->rules as $attribute => $rules)
|
foreach ($this->rules as $attribute => $rules)
|
||||||
{
|
{
|
||||||
foreach ($rules as $rule)
|
foreach ($rules as $rule) $this->check($attribute, $rule);
|
||||||
{
|
|
||||||
$this->check($attribute, $rule);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return count($this->errors->messages) == 0;
|
return count($this->errors->messages) == 0;
|
||||||
|
@ -159,29 +156,35 @@ protected function check($attribute, $rule)
|
||||||
{
|
{
|
||||||
list($rule, $parameters) = $this->parse($rule);
|
list($rule, $parameters) = $this->parse($rule);
|
||||||
|
|
||||||
if ( ! method_exists($this, $validator = 'validate_'.$rule) and ! isset(static::$validators[$rule]))
|
// Verify that the attribute and rule combination is actually validatable before
|
||||||
{
|
// attempting to call the validation rule. Unless the rule implicitly requires
|
||||||
throw new \Exception("Validation rule [$rule] doesn't exist.");
|
// 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
|
if ( ! $this->{'validate_'.$rule}($attribute, $value, $parameters, $this))
|
||||||
// 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))
|
|
||||||
{
|
{
|
||||||
$this->error($attribute, $rule, $parameters);
|
$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.
|
* Add an error message to the validator's collection of messages.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue