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

Commit 8e568b4

Browse files
committed
Fix for #52
1 parent 5af4d67 commit 8e568b4

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/Components/FormCheckbox.php

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

33
namespace ProtoneMedia\LaravelFormComponents\Components;
44

5+
use Illuminate\Contracts\Support\Arrayable;
56
use Illuminate\Support\Arr;
67
use Illuminate\Support\Str;
78

@@ -42,6 +43,10 @@ public function __construct(
4243
if (!session()->hasOldInput() && $this->isNotWired()) {
4344
$boundValue = $this->getBoundValue($bind, $inputName);
4445

46+
if ($boundValue instanceof Arrayable) {
47+
$boundValue = $boundValue->toArray();
48+
}
49+
4550
if (is_array($boundValue)) {
4651
$this->checked = in_array($value, $boundValue);
4752
return;

tests/Feature/ChekboxTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public function it_check_the_right_element_as_default()
1717
->seeElement('input[value="b"]:not(:checked)');
1818
}
1919

20+
/** @test */
21+
public function it_supports_bound_collections()
22+
{
23+
$this->registerTestRoute('checkbox-collection');
24+
25+
$this->visit('/checkbox-collection')
26+
->seeElement('input[value="read"]:checked')
27+
->seeElement('input[value="write"]:not(:checked)');
28+
}
29+
2030
/** @test */
2131
public function it_does_check_the_right_input_element_after_a_validation_error()
2232
{
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@php
2+
$target = ['permissions' => collect(['read'])]
3+
@endphp
4+
5+
<x-form>
6+
@bind($target)
7+
<x-form-group>
8+
<x-form-checkbox name="permissions[]" value="read" />
9+
<x-form-checkbox name="permissions[]" value="write" />
10+
</x-form-group>
11+
@endbind
12+
13+
<x-form-submit />
14+
</x-form>

0 commit comments

Comments
 (0)