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

Commit bf4fe34

Browse files
committed
Fix for #20
1 parent 8f2d38f commit bf4fe34

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable changes to `laravel-form-components` will be documented in this file
44

5+
## 2.1.2 - 2020-09-29
6+
7+
- Bugfix for multiple checkbox/radio elements with the same name
8+
9+
## 2.1.1 - 2020-09-29
10+
11+
- Bugfix for select options with numeric keys
12+
513
## 2.1.0 - 2020-09-14
614

715
- Select elements now support a slot

src/Components/FormCheckbox.php

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

33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

5+
use Illuminate\Support\Arr;
6+
use Illuminate\Support\Str;
7+
58
class FormCheckbox extends Component
69
{
710
use HandlesValidationErrors;
@@ -30,12 +33,19 @@ public function __construct(
3033
$this->value = $value;
3134
$this->showErrors = $showErrors;
3235

33-
if (old($name)) {
34-
$this->checked = true;
36+
$inputName = Str::before($name, '[]');
37+
38+
if ($oldData = old($inputName)) {
39+
$this->checked = in_array($value, Arr::wrap($oldData));
3540
}
3641

3742
if (!session()->hasOldInput() && $this->isNotWired()) {
38-
$boundValue = $this->getBoundValue($bind, $name);
43+
$boundValue = $this->getBoundValue($bind, $inputName);
44+
45+
if (is_array($boundValue)) {
46+
$this->checked = in_array($value, $boundValue);
47+
return;
48+
}
3949

4050
$this->checked = is_null($boundValue) ? $default : $boundValue;
4151
}

tests/Feature/ChekboxTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace ProtoneMedia\LaravelFormComponents\Tests\Feature;
4+
5+
use Illuminate\Http\Request;
6+
use ProtoneMedia\LaravelFormComponents\Tests\TestCase;
7+
8+
class ChekboxTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_check_the_right_element_as_default()
12+
{
13+
$this->registerTestRoute('default-checkbox');
14+
15+
$this->visit('/default-checkbox')
16+
->seeElement('input[value="a"]:checked')
17+
->seeElement('input[value="b"]:not(:checked)');
18+
}
19+
20+
/** @test */
21+
public function it_does_check_the_right_input_element_after_a_validation_error()
22+
{
23+
$this->registerTestRoute('checkbox-validation', function (Request $request) {
24+
$data = $request->validate([
25+
'checkbox' => 'required|array',
26+
'checkbox.*' => 'in:a',
27+
]);
28+
});
29+
30+
$this->visit('/checkbox-validation?check=b')
31+
->seeElement('input[value="a"]:not(:checked)')
32+
->seeElement('input[value="b"]:checked')
33+
->press('Submit')
34+
->seeElement('input[value="a"]:not(:checked)')
35+
->seeElement('input[value="b"]:checked');
36+
}
37+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<x-form action="/checkbox-validation">
2+
<x-form-group>
3+
<x-form-checkbox name="checkbox[]" value="a" :default="request()->query('check') == 'a'" />
4+
</x-form-group>
5+
6+
<x-form-group>
7+
<x-form-checkbox name="checkbox[]" value="b" :default="request()->query('check') == 'b'" />
8+
</x-form-group>
9+
10+
<x-form-submit />
11+
</x-form>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@php
2+
$target = ['checkbox' => ['a']];
3+
@endphp
4+
5+
<x-form>
6+
@bind($target)
7+
<x-form-group>
8+
<x-form-checkbox name="checkbox[]" value="a" />
9+
</x-form-group>
10+
11+
<x-form-group>
12+
<x-form-checkbox name="checkbox[]" value="b" />
13+
</x-form-group>
14+
@endbind
15+
16+
<x-form-submit />
17+
</x-form>

0 commit comments

Comments
 (0)