diff --git a/src/Elements/Form.php b/src/Elements/Form.php index 1573120..e717758 100644 --- a/src/Elements/Form.php +++ b/src/Elements/Form.php @@ -16,6 +16,7 @@ use Illuminate\Session\Store; use Illuminate\Support\Arr; use Illuminate\Support\HtmlString; +use Illuminate\Support\MessageBag; use Illuminate\Support\Str; use Illuminate\Support\ViewErrorBag; use stdClass; @@ -102,13 +103,13 @@ class Form extends \Galahad\Aire\DTD\Form implements NonInput * @var string */ protected $form_request; - + /** * Custom error bag * * @var string */ - protected $error_bag = null; + protected $error_bag = 'default'; /** * Set to true to load development versions of JS @@ -330,28 +331,16 @@ public function getBoundValue($name, $default = null) /** * Get any validation errors associated with an Element * - * @param string $name - * @return array + * @param ?string $name + * @return MessageBag|array */ - public function getErrors(string $name): array + public function getErrors(string $name = null) { - if (!$errors = $this->session_store->get('errors')) { - return []; - } + $errors = $this->session_store + ->get('errors', new ViewErrorBag()) + ->getBag($this->error_bag); - if (!$errors instanceof ViewErrorBag) { - return []; - } - - if ($this->error_bag) { - $errors = $errors->getBag($this->error_bag); - } - - if (!$errors->has($name)) { - return []; - } - - return $errors->get($name); + return $name ? $errors->get($name) : $errors; } /** @@ -417,7 +406,13 @@ public function closeButton(): Button return $button; } - + + /** + * Set the name of the error bag to use for error messages + * + * @param string $name + * @return $this + */ public function errorBag($name): self { $this->error_bag = $name; diff --git a/src/Elements/Summary.php b/src/Elements/Summary.php index 89da870..8623b90 100644 --- a/src/Elements/Summary.php +++ b/src/Elements/Summary.php @@ -20,6 +20,7 @@ public function __construct(Aire $aire, Form $form = null) parent::__construct($aire, $form); $this->view_data['verbose'] = $aire->config('verbose_summaries_by_default', false); + $this->view_data['errors'] = $form->getErrors(); } public function verbose(bool $verbose = true): self diff --git a/views/summary.blade.php b/views/summary.blade.php index e9a63b8..1e2df51 100644 --- a/views/summary.blade.php +++ b/views/summary.blade.php @@ -1,5 +1,5 @@ - + @if (isset($errors) && $errors->any())