Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errorBag() form option #134

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
Loading