Skip to content

Commit

Permalink
added errors() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor Parks authored and Connor Parks committed Jun 16, 2015
1 parent 00eecd6 commit 3a49cb8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Herbert/Framework/Providers/HerbertServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public function register()
'notifier',
'Herbert\Framework\Notifier'
);

$this->app->singleton(
'errors',
function ()
{
return session_flashed('__validation_errors', []);
}
);

$_GLOBALS['errors'] = $this->app['errors'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Herbert/Framework/Providers/TwigServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function constructTwig()
$twig->addGlobal($key, $value);
}

$twig->addGlobal('errors', session()->getFlashBag()->get('__validation_errors', []));
$twig->addGlobal('errors', $this->app['errors']);

foreach ((array) $this->app['twig.functions'] as $function)
{
Expand Down
22 changes: 22 additions & 0 deletions bootstrap/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ function herbert($binding = null)
}
}

if ( ! function_exists('errors'))
{
/**
* Get the errors.
*
* @param string key
* @return array
*/
function errors($key = null)
{
$errors = herbert('errors');
$errors = isset($errors[0]) ? $errors[0] : $errors;

if (!$key)
{
return $errors;
}

return array_get($errors, $key);
}
}

if ( ! function_exists('session'))
{
/**
Expand Down

0 comments on commit 3a49cb8

Please sign in to comment.