Skip to content

Commit

Permalink
Summary element should refer to custom error bag (#135)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
cheyner and inxilpro authored Sep 6, 2024
1 parent 8f5f85d commit 45762bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
39 changes: 17 additions & 22 deletions src/Elements/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Elements/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion views/summary.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php /** @var \Galahad\Aire\Elements\Attributes\Attributes $attributes */ ?>
<?php /** @var \Illuminate\Support\ViewErrorBag $errors */ ?>
<?php /** @var \Illuminate\Support\MessageBag $errors */ ?>

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

Expand Down

0 comments on commit 45762bb

Please sign in to comment.