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

Commit 90fd8e6

Browse files
fix(binding): Ensure nested properties uses dot notation (#59)
1 parent 69228e2 commit 90fd8e6

6 files changed

+47
-6
lines changed

src/Components/FormCheckbox.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public function __construct(
3434
$this->value = $value;
3535
$this->showErrors = $showErrors;
3636

37-
$inputName = Str::before($name, '[]');
37+
$inputName = static::convertBracketsToDots(Str::before($name, '[]'));
3838

39-
if ($oldData = old(static::convertBracketsToDots($inputName))) {
39+
if ($oldData = old($inputName)) {
4040
$this->checked = in_array($value, Arr::wrap($oldData));
4141
}
4242

src/Components/FormRadio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(
3232
}
3333

3434
if (!session()->hasOldInput() && $this->isNotWired()) {
35-
$boundValue = $this->getBoundValue($bind, $name);
35+
$boundValue = $this->getBoundValue($bind, $inputName);
3636

3737
if (!is_null($boundValue)) {
3838
$this->checked = $boundValue == $this->value;

src/Components/FormSelect.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ public function __construct(
4343
$this->placeholder = $placeholder;
4444

4545
if ($this->isNotWired()) {
46-
$inputName = Str::before($name, '[]');
46+
$inputName = static::convertBracketsToDots(Str::before($name, '[]'));
4747

4848
$default = $this->getBoundValue($bind, $inputName) ?: $default;
4949

50-
$this->selectedKey = old(static::convertBracketsToDots($inputName), $default);
50+
$this->selectedKey = old($inputName, $default);
5151

5252
if ($this->selectedKey instanceof Arrayable) {
5353
$this->selectedKey = $this->selectedKey->toArray();

src/Components/HandlesDefaultAndOldValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private function setValue(
1919
$inputName = static::convertBracketsToDots($name);
2020

2121
if (!$language) {
22-
$boundValue = $this->getBoundValue($bind, $name);
22+
$boundValue = $this->getBoundValue($bind, $inputName);
2323

2424
$default = is_null($boundValue) ? $default : $boundValue;
2525

tests/Feature/BindTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,21 @@ public function it_overrides_the_default_value()
103103
->dontSeeElement('input[name="radio"]:checked');
104104
}
105105

106+
/** @test */
107+
public function it_overrides_the_default_value_when_nested()
108+
{
109+
$this->registerTestRoute('default-values-with-nested-bound-target');
110+
111+
$this->visit('/default-values-with-nested-bound-target')
112+
->seeElement('input[name="nested[input]"][value="a"]')
113+
->seeInElement('textarea[name="nested[textarea]"]', 'b')
114+
->seeElement('select[name="nested[select]"] > option[value="c"]:selected')
115+
->seeElement('input[name="nested[checkbox]"]')
116+
->dontSeeElement('input[name="nested[checkbox]"]:checked')
117+
->seeElement('input[name="nested[radio]"]')
118+
->dontSeeElement('input[name="nested[radio]"]:checked');
119+
}
120+
106121
/** @test */
107122
public function it_can_bind_two_targets_to_the_form()
108123
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@php
2+
$target = [
3+
'nested' => [
4+
'input' => 'a',
5+
'textarea' => 'b',
6+
'select' => 'c',
7+
'checkbox' => false,
8+
'radio' => false,
9+
]
10+
];
11+
@endphp
12+
13+
<x-form>
14+
@bind($target)
15+
<x-form-input default="d" name="nested[input]" />
16+
<x-form-textarea default="e" name="nested[textarea]" />
17+
<x-form-select default="f" name="nested[select]" :options="['' => '', 'c' => 'c']" />
18+
<x-form-checkbox :default="true" name="nested[checkbox]" />
19+
20+
<x-form-group name="radio">
21+
<x-form-radio :default="true" name="nested[radio]" />
22+
</x-form-group>
23+
24+
<x-form-submit />
25+
@endbind
26+
</x-form>

0 commit comments

Comments
 (0)