removed error collector bloat, switched to plain array. thanks mikelbring.

This commit is contained in:
Taylor Otwell 2011-06-25 20:05:20 -05:00
parent 4a5190dfd9
commit b7b258a10b
5 changed files with 10 additions and 103 deletions

View File

@ -21,7 +21,7 @@ public static function entities($value)
*/
public static function script($url)
{
return '<script type="text/javascript" src="'.trim(static::entities(URL::to_asset($url)), '.js').'.js"></script>'.PHP_EOL;
return '<script type="text/javascript" src="'.static::entities(URL::to_asset($url)).'"></script>'.PHP_EOL;
}
/**
@ -32,7 +32,7 @@ public static function script($url)
*/
public static function style($url, $media = 'all')
{
return '<link href="'.trim(static::entities(URL::to_asset($url)), '.css').'.css" rel="stylesheet" type="text/css" media="'.$media.'" />'.PHP_EOL;
return '<link href="'.static::entities(URL::to_asset($url)).'" rel="stylesheet" type="text/css" media="'.$media.'" />'.PHP_EOL;
}
/**

View File

@ -1,81 +0,0 @@
<?php namespace System\Validation;
class Error_Collector {
/**
* All of the error messages.
*
* @var array
*/
public $messages;
/**
* Create a new Error Collector instance.
*
* @return void
*/
public function __construct($messages = array())
{
$this->messages = $messages;
}
/**
* Add an error message to the collector.
*
* @param string $attribute
* @param string $message
* @return void
*/
public function add($attribute, $message)
{
$this->messages[$attribute][] = $message;
}
/**
* Determine if errors exist for an attribute.
*
* @param string $attribute
* @return bool
*/
public function has($attribute)
{
return $this->first($attribute) !== '';
}
/**
* Get the first error message for an attribute.
*
* @param string $attribute
* @return string
*/
public function first($attribute)
{
return (count($messages = $this->get($attribute)) > 0) ? $messages[0] : '';
}
/**
* Get all of the error messages for an attribute.
*
* If no attribute is specified, all of the error messages will be returned.
*
* @param string $attribute
* @return array
*/
public function get($attribute = null)
{
if (is_null($attribute))
{
$all = array();
foreach ($this->messages as $messages)
{
$all = array_merge($all, $messages);
}
return $all;
}
return (array_key_exists($attribute, $this->messages)) ? $this->messages[$attribute] : array();
}
}

View File

@ -33,16 +33,16 @@ public function __construct($attributes)
* Run the validation rule.
*
* @param array $attributes
* @param Error_Collector $errors
* @param array $errors
* @return void
*/
public function validate($attributes, $errors)
public function validate($attributes, &$errors)
{
foreach ($this->attributes as $attribute)
{
if ( ! $this->check($attribute, $attributes))
{
$errors->add($attribute, $this->prepare_message($attribute));
$errors[$attribute][] = $this->prepare_message($attribute);
}
}
}

View File

@ -10,9 +10,9 @@ class Validator {
public $attributes;
/**
* The validation error collector.
* The validation errors
*
* @var Error_Collector
* @var array
*/
public $errors;
@ -36,8 +36,6 @@ public function __construct($target)
// attributes as the validation attributes.
// ---------------------------------------------------------
$this->attributes = ($target instanceof DB\Eloquent) ? $target->attributes : (array) $target;
$this->errors = new Validation\Error_Collector;
}
/**
@ -58,7 +56,7 @@ public static function of($target)
*/
public function is_valid()
{
$this->errors->messages = array();
$this->errors = array();
foreach ($this->rules as $rule)
{
@ -69,7 +67,7 @@ public function is_valid()
$rule->validate($this->attributes, $this->errors);
}
return count($this->errors->messages) == 0;
return count($this->errors) == 0;
}
/**

View File

@ -34,16 +34,6 @@ public function __construct($view, $data = array())
{
$this->view = $view;
$this->data = $data;
// -----------------------------------------------------
// Every view has an error collector. This makes it
// convenient to check for any validation errors without
// worrying if the error collector is instantiated.
//
// If an error collector is in the session, it will
// be used as the error collector for the view.
// -----------------------------------------------------
$this->data['errors'] = (Config::get('session.driver') != '' and Session::has('errors')) ? Session::get('errors') : new Validation\Error_Collector;
}
/**