refactoring validator.

This commit is contained in:
Taylor Otwell 2011-10-16 13:25:37 -05:00
parent 48f1879b9f
commit 8bd6d34622
1 changed files with 14 additions and 14 deletions

View File

@ -1,6 +1,7 @@
<?php namespace Laravel\Validation; <?php namespace Laravel\Validation;
use Closure; use Closure;
use Laravel\Arr;
use Laravel\IoC; use Laravel\IoC;
use Laravel\Str; use Laravel\Str;
use Laravel\Lang; use Laravel\Lang;
@ -51,13 +52,6 @@ class Validator {
*/ */
protected $language; protected $language;
/**
* The registered custom validators.
*
* @var array
*/
protected static $validators = array();
/** /**
* The size related validation rules. * The size related validation rules.
* *
@ -72,6 +66,13 @@ class Validator {
*/ */
protected $numeric_rules = array('numeric', 'integer'); protected $numeric_rules = array('numeric', 'integer');
/**
* The registered custom validators.
*
* @var array
*/
protected static $validators = array();
/** /**
* Create a new validator instance. * Create a new validator instance.
* *
@ -165,12 +166,15 @@ protected function check($attribute, $rule)
// Extract the actual value for the attribute. We don't want every rule // Extract the actual value for the attribute. We don't want every rule
// to worry about obtaining the value from the array of attributes. // to worry about obtaining the value from the array of attributes.
$value = (isset($this->attributes[$attribute])) ? $this->attributes[$attribute] : null; $value = Arr::get($this->attributes, $attribute);
// No validation will be run for attributes that do not exist unless the // No validation will be run for attributes that do not exist unless the
// rule being validated is "required" or "accepted". No other rules have // rule being validated is "required" or "accepted". No other rules have
// implicit "required" checks for validation. // implicit "required" checks for validation.
if ( ! $this->validate_required($attribute) and ! in_array($rule, array('required', 'accepted'))) return; if ( ! $this->validate_required($attribute, $value) and ! in_array($rule, array('required', 'accepted')))
{
return;
}
if ( ! $this->$validator($attribute, $value, $parameters, $this)) if ( ! $this->$validator($attribute, $value, $parameters, $this))
{ {
@ -204,11 +208,7 @@ protected function error($attribute, $rule, $parameters)
*/ */
protected function validate_required($attribute, $value) protected function validate_required($attribute, $value)
{ {
if (is_null($value)) return false; return (is_null($value) or (is_string($value) and trim($value) === ''));
if (is_string($value) and trim($value) === '') return false;
return true;
} }
/** /**