Skip to content

Commit 45762bb

Browse files
cheynerinxilpro
andauthored
Summary element should refer to custom error bag (#135)
* Summary element should refer to custom error bag * Using getBag() and style fix * Centralize error bag logic to Form * Code style * Revert visibility * Types --------- Co-authored-by: Chris Morrell <[email protected]>
1 parent 8f5f85d commit 45762bb

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

src/Elements/Form.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Session\Store;
1717
use Illuminate\Support\Arr;
1818
use Illuminate\Support\HtmlString;
19+
use Illuminate\Support\MessageBag;
1920
use Illuminate\Support\Str;
2021
use Illuminate\Support\ViewErrorBag;
2122
use stdClass;
@@ -102,13 +103,13 @@ class Form extends \Galahad\Aire\DTD\Form implements NonInput
102103
* @var string
103104
*/
104105
protected $form_request;
105-
106+
106107
/**
107108
* Custom error bag
108109
*
109110
* @var string
110111
*/
111-
protected $error_bag = null;
112+
protected $error_bag = 'default';
112113

113114
/**
114115
* Set to true to load development versions of JS
@@ -330,28 +331,16 @@ public function getBoundValue($name, $default = null)
330331
/**
331332
* Get any validation errors associated with an Element
332333
*
333-
* @param string $name
334-
* @return array
334+
* @param ?string $name
335+
* @return MessageBag|array
335336
*/
336-
public function getErrors(string $name): array
337+
public function getErrors(string $name = null)
337338
{
338-
if (!$errors = $this->session_store->get('errors')) {
339-
return [];
340-
}
339+
$errors = $this->session_store
340+
->get('errors', new ViewErrorBag())
341+
->getBag($this->error_bag);
341342

342-
if (!$errors instanceof ViewErrorBag) {
343-
return [];
344-
}
345-
346-
if ($this->error_bag) {
347-
$errors = $errors->getBag($this->error_bag);
348-
}
349-
350-
if (!$errors->has($name)) {
351-
return [];
352-
}
353-
354-
return $errors->get($name);
343+
return $name ? $errors->get($name) : $errors;
355344
}
356345

357346
/**
@@ -417,7 +406,13 @@ public function closeButton(): Button
417406

418407
return $button;
419408
}
420-
409+
410+
/**
411+
* Set the name of the error bag to use for error messages
412+
*
413+
* @param string $name
414+
* @return $this
415+
*/
421416
public function errorBag($name): self
422417
{
423418
$this->error_bag = $name;

src/Elements/Summary.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function __construct(Aire $aire, Form $form = null)
2020
parent::__construct($aire, $form);
2121

2222
$this->view_data['verbose'] = $aire->config('verbose_summaries_by_default', false);
23+
$this->view_data['errors'] = $form->getErrors();
2324
}
2425

2526
public function verbose(bool $verbose = true): self

views/summary.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php /** @var \Galahad\Aire\Elements\Attributes\Attributes $attributes */ ?>
2-
<?php /** @var \Illuminate\Support\ViewErrorBag $errors */ ?>
2+
<?php /** @var \Illuminate\Support\MessageBag $errors */ ?>
33

44
@if (isset($errors) && $errors->any())
55

0 commit comments

Comments
 (0)