Skip to content
This repository was archived by the owner on Jan 2, 2024. It is now read-only.

Commit 13de523

Browse files
committed
Error bag support
1 parent bf4fe34 commit 13de523

File tree

9 files changed

+71
-14
lines changed

9 files changed

+71
-14
lines changed

resources/views/bootstrap-4/form-errors.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@error($name)
1+
@error($name, $bag)
22
<div {!! $attributes->merge(['class' => 'invalid-feedback']) !!}>
33
{{ $message }}
44
</div>

resources/views/tailwind/form-errors.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@error($name)
1+
@error($name, $bag)
22
<p {!! $attributes->merge(['class' => 'text-red-500 text-xs italic']) !!}>
33
{{ $message }}
44
</p>

src/Components/FormErrors.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

5+
use Illuminate\Support\Str;
6+
57
class FormErrors extends Component
68
{
79
public string $name;
10+
public string $bag;
811

912
/**
1013
* Create a new component instance.
1114
*
1215
* @return void
1316
*/
14-
public function __construct(string $name)
17+
public function __construct(string $name, string $bag = 'default')
1518
{
16-
$this->name = str_replace(['[', ']'], ['.', ''], $name);
19+
$this->name = str_replace(['[', ']'], ['.', ''], Str::before($name, '[]'));
20+
21+
$this->bag = $bag;
1722
}
1823
}

src/Components/FormSelect.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

55
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
67

78
class FormSelect extends Component
89
{
@@ -34,9 +35,11 @@ public function __construct(
3435
$this->options = $options;
3536

3637
if ($this->isNotWired()) {
37-
$default = $this->getBoundValue($bind, $name) ?: $default;
38+
$inputName = Str::before($name, '[]');
3839

39-
$this->selectedKey = old($name, $default);
40+
$default = $this->getBoundValue($bind, $inputName) ?: $default;
41+
42+
$this->selectedKey = old($inputName, $default);
4043
}
4144

4245
$this->multiple = $multiple;

src/Components/HandlesValidationErrors.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

5+
use Illuminate\Contracts\Support\MessageBag;
56
use Illuminate\Support\Facades\View;
7+
use Illuminate\Support\Str;
68
use Illuminate\Support\ViewErrorBag;
79

810
trait HandlesValidationErrors
@@ -24,6 +26,19 @@ public function hasErrorAndShow(string $name, string $bag = 'default'): bool
2426
: false;
2527
}
2628

29+
/**
30+
* Getter for the ErrorBag.
31+
*
32+
* @param string $bag
33+
* @return \Illuminate\Contracts\Support\MessageBag
34+
*/
35+
protected function getErrorBag(string $bag = 'default'): MessageBag
36+
{
37+
$bags = View::shared('errors', fn () => request()->session()->get('errors', new ViewErrorBag));
38+
39+
return $bags->getBag($bag);
40+
}
41+
2742
/**
2843
* Returns a boolean wether the given attribute has an error.
2944
*
@@ -33,10 +48,10 @@ public function hasErrorAndShow(string $name, string $bag = 'default'): bool
3348
*/
3449
public function hasError(string $name, string $bag = 'default'): bool
3550
{
36-
$errors = View::shared('errors', fn () => request()->session()->get('errors', new ViewErrorBag));
51+
$name = str_replace(['[', ']'], ['.', ''], Str::before($name, '[]'));
3752

38-
$name = str_replace(['[', ']'], ['.', ''], $name);
53+
$errorBag = $this->getErrorBag($bag);
3954

40-
return $errors->getBag($bag)->has($name);
55+
return $errorBag->has($name) || $errorBag->has($name . '.*');
4156
}
4257
}

tests/Feature/ChekboxTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public function it_check_the_right_element_as_default()
2121
public function it_does_check_the_right_input_element_after_a_validation_error()
2222
{
2323
$this->registerTestRoute('checkbox-validation', function (Request $request) {
24-
$data = $request->validate([
24+
$request->validate([
2525
'checkbox' => 'required|array',
2626
'checkbox.*' => 'in:a',
2727
]);
2828
});
2929

30-
$this->visit('/checkbox-validation?check=b')
30+
$this->visit('/checkbox-validation')
3131
->seeElement('input[value="a"]:not(:checked)')
3232
->seeElement('input[value="b"]:checked')
3333
->press('Submit')

tests/Feature/MultipleSelectTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;
4+
5+
use Illuminate\Http\Request;
6+
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
7+
8+
class MultipleSelectTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_posts_all_selected_options()
12+
{
13+
$this->registerTestRoute('multiple-select-keys', function (Request $request) {
14+
$request->validate([
15+
'select' => 'required|string',
16+
]);
17+
});
18+
19+
$this->visit('/multiple-select-keys?both=yes')
20+
->seeElement('option[value="be"]:selected')
21+
->seeElement('option[value="nl"]:selected')
22+
->press('Submit')
23+
->seeElement('option[value="be"]:selected')
24+
->seeElement('option[value="nl"]:selected')
25+
->seeText('The select must be a string.');
26+
}
27+
}

tests/Feature/views/checkbox-validation.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<x-form action="/checkbox-validation">
1+
<x-form>
22
<x-form-group>
3-
<x-form-checkbox name="checkbox[]" value="a" :default="request()->query('check') == 'a'" />
3+
<x-form-checkbox name="checkbox[]" value="a" />
44
</x-form-group>
55

66
<x-form-group>
7-
<x-form-checkbox name="checkbox[]" value="b" :default="request()->query('check') == 'b'" />
7+
<x-form-checkbox name="checkbox[]" value="b" :default="old() ? false : true" />
88
</x-form-group>
99

1010
<x-form-submit />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<x-form>
2+
<x-form-select multiple name="select[]" :default="(old() ? null : ['be', 'nl'])"
3+
:options="['be' => 'Belgium', 'nl' => 'The Netherlands']" />
4+
5+
<x-form-input name="another_field" />
6+
<x-form-submit />
7+
</x-form>

0 commit comments

Comments
 (0)