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

fix(binding): Ensure nested properties uses dot notation #59

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Components/FormCheckbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
2 changes: 1 addition & 1 deletion src/Components/FormRadio.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Components/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Components/HandlesDefaultAndOldValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/BindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@php
$target = [
'nested' => [
'input' => 'a',
'textarea' => 'b',
'select' => 'c',
'checkbox' => false,
'radio' => false,
]
];
@endphp

<x-form>
@bind($target)
<x-form-input default="d" name="nested[input]" />
<x-form-textarea default="e" name="nested[textarea]" />
<x-form-select default="f" name="nested[select]" :options="['' => '', 'c' => 'c']" />
<x-form-checkbox :default="true" name="nested[checkbox]" />

<x-form-group name="radio">
<x-form-radio :default="true" name="nested[radio]" />
</x-form-group>

<x-form-submit />
@endbind
</x-form>