Skip to content

Commit

Permalink
errorBag() form option (#134)
Browse files Browse the repository at this point in the history
* errorBag() method/functionality

* use getBag

* Add a test

---------

Co-authored-by: Chris Morrell <[email protected]>
  • Loading branch information
cheyner and inxilpro authored Aug 13, 2024
1 parent d78116e commit 054766a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Elements/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ class Form extends \Galahad\Aire\DTD\Form implements NonInput
* @var string
*/
protected $form_request;

/**
* Custom error bag
*
* @var string
*/
protected $error_bag = null;

/**
* Set to true to load development versions of JS
Expand Down Expand Up @@ -335,6 +342,10 @@ public function getErrors(string $name): array
if (!$errors instanceof ViewErrorBag) {
return [];
}

if ($this->error_bag) {
$errors = $errors->getBag($this->error_bag);
}

if (!$errors->has($name)) {
return [];
Expand Down Expand Up @@ -406,6 +417,13 @@ public function closeButton(): Button

return $button;
}

public function errorBag($name): self
{
$this->error_bag = $name;

return $this;
}

/**
* Set the form's action to a named route
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/ServerValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,33 @@ public function test_validation_errors_are_shown_on_render()
$this->assertSelectorExists($html, '[data-aire-component="errors"]');
$this->assertSelectorContainsText($html, '[data-aire-component="errors"]', 'The generic input field is required');
}

public function test_it_respects_error_bags()
{
Route::get('/aire', function() {
return view('basic-form');
})->middleware('web');

Route::post('/default-bag', function(Request $request) {
$request->validate([
'generic_input' => 'required',
]);
})->middleware('web');

Route::post('/bag2', function(Request $request) {
$request->validateWithBag('bag2', [
'generic_input' => 'required',
]);
})->middleware('web');

$this->post('/default-bag')->assertRedirect();
$html = $this->get('/aire')->getContent();

$this->assertSelectorClassNames($html, '#generic_input_group', 'is-invalid');

$this->post('/bag2')->assertRedirect();
$html = $this->get('/aire')->getContent();

$this->assertSelectorMissingClassNames($html, '#generic_input_group', 'is-invalid');
}
}

0 comments on commit 054766a

Please sign in to comment.