diff --git a/src/Components/FormCheckbox.php b/src/Components/FormCheckbox.php index fa22cb6..99a46dd 100644 --- a/src/Components/FormCheckbox.php +++ b/src/Components/FormCheckbox.php @@ -34,9 +34,9 @@ public function __construct( $this->value = $value; $this->showErrors = $showErrors; - $inputName = Str::before($name, '[]'); + $inputName = static::convertBracketsToDots(Str::before($name, '[]')); - if ($oldData = old(static::convertBracketsToDots($inputName))) { + if ($oldData = old($inputName)) { $this->checked = in_array($value, Arr::wrap($oldData)); } diff --git a/src/Components/FormRadio.php b/src/Components/FormRadio.php index d295ab0..1def1c1 100644 --- a/src/Components/FormRadio.php +++ b/src/Components/FormRadio.php @@ -32,7 +32,7 @@ public function __construct( } if (!session()->hasOldInput() && $this->isNotWired()) { - $boundValue = $this->getBoundValue($bind, $name); + $boundValue = $this->getBoundValue($bind, $inputName); if (!is_null($boundValue)) { $this->checked = $boundValue == $this->value; diff --git a/src/Components/FormSelect.php b/src/Components/FormSelect.php index 45d7b5d..0142ec8 100644 --- a/src/Components/FormSelect.php +++ b/src/Components/FormSelect.php @@ -43,11 +43,11 @@ public function __construct( $this->placeholder = $placeholder; if ($this->isNotWired()) { - $inputName = Str::before($name, '[]'); + $inputName = static::convertBracketsToDots(Str::before($name, '[]')); $default = $this->getBoundValue($bind, $inputName) ?: $default; - $this->selectedKey = old(static::convertBracketsToDots($inputName), $default); + $this->selectedKey = old($inputName, $default); if ($this->selectedKey instanceof Arrayable) { $this->selectedKey = $this->selectedKey->toArray(); diff --git a/src/Components/HandlesDefaultAndOldValue.php b/src/Components/HandlesDefaultAndOldValue.php index 74bd116..1c57e3a 100644 --- a/src/Components/HandlesDefaultAndOldValue.php +++ b/src/Components/HandlesDefaultAndOldValue.php @@ -19,7 +19,7 @@ private function setValue( $inputName = static::convertBracketsToDots($name); if (!$language) { - $boundValue = $this->getBoundValue($bind, $name); + $boundValue = $this->getBoundValue($bind, $inputName); $default = is_null($boundValue) ? $default : $boundValue; diff --git a/tests/Feature/BindTest.php b/tests/Feature/BindTest.php index abd82f4..884cfea 100644 --- a/tests/Feature/BindTest.php +++ b/tests/Feature/BindTest.php @@ -103,6 +103,21 @@ public function it_overrides_the_default_value() ->dontSeeElement('input[name="radio"]:checked'); } + /** @test */ + public function it_overrides_the_default_value_when_nested() + { + $this->registerTestRoute('default-values-with-nested-bound-target'); + + $this->visit('/default-values-with-nested-bound-target') + ->seeElement('input[name="nested[input]"][value="a"]') + ->seeInElement('textarea[name="nested[textarea]"]', 'b') + ->seeElement('select[name="nested[select]"] > option[value="c"]:selected') + ->seeElement('input[name="nested[checkbox]"]') + ->dontSeeElement('input[name="nested[checkbox]"]:checked') + ->seeElement('input[name="nested[radio]"]') + ->dontSeeElement('input[name="nested[radio]"]:checked'); + } + /** @test */ public function it_can_bind_two_targets_to_the_form() { diff --git a/tests/Feature/views/default-values-with-nested-bound-target.blade.php b/tests/Feature/views/default-values-with-nested-bound-target.blade.php new file mode 100644 index 0000000..7dace12 --- /dev/null +++ b/tests/Feature/views/default-values-with-nested-bound-target.blade.php @@ -0,0 +1,26 @@ +@php + $target = [ + 'nested' => [ + 'input' => 'a', + 'textarea' => 'b', + 'select' => 'c', + 'checkbox' => false, + 'radio' => false, + ] + ]; +@endphp + + + @bind($target) + + + + + + + + + + + @endbind +