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

Commit c0eb477

Browse files
authored
Bugfix for 'old' nested data (#49)
1 parent 4ffc990 commit c0eb477

File tree

9 files changed

+66
-7
lines changed

9 files changed

+66
-7
lines changed

CHANGELOG.md

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

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

5+
## 2.5.4 - 2020-02-15
6+
7+
- Bugfix for old nested data
8+
59
## 2.5.3 - 2020-02-11
610

711
- Bugfix for setting radio elements as checked/default

src/Components/Component.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,15 @@ protected function generateIdByName(): string
8888
{
8989
return "auto_id_" . $this->name;
9090
}
91+
92+
/**
93+
* Converts a bracket-notation to a dotted-notation
94+
*
95+
* @param string $name
96+
* @return string
97+
*/
98+
protected static function convertBracketsToDots($name): string
99+
{
100+
return str_replace(['[', ']'], ['.', ''], $name);
101+
}
91102
}

src/Components/FormCheckbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535

3636
$inputName = Str::before($name, '[]');
3737

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

src/Components/FormErrors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FormErrors extends Component
1616
*/
1717
public function __construct(string $name, string $bag = 'default')
1818
{
19-
$this->name = str_replace(['[', ']'], ['.', ''], Str::before($name, '[]'));
19+
$this->name = static::convertBracketsToDots(Str::before($name, '[]'));
2020

2121
$this->bag = $bag;
2222
}

src/Components/FormRadio.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ public function __construct(
2525
$this->value = $value;
2626
$this->showErrors = $showErrors;
2727

28-
if (old($name)) {
29-
$this->checked = old($name) == $value;
28+
$inputName = static::convertBracketsToDots($name);
29+
30+
if (old($inputName)) {
31+
$this->checked = old($inputName) == $value;
3032
}
3133

3234
if (!session()->hasOldInput() && $this->isNotWired()) {

src/Components/FormSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(
4242

4343
$default = $this->getBoundValue($bind, $inputName) ?: $default;
4444

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

4747
if ($this->selectedKey instanceof Arrayable) {
4848
$this->selectedKey = $this->selectedKey->toArray();

src/Components/HandlesDefaultAndOldValue.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ private function setValue(
1616
return;
1717
}
1818

19+
$inputName = static::convertBracketsToDots($name);
20+
1921
if (!$language) {
2022
$default = $this->getBoundValue($bind, $name) ?: $default;
2123

22-
return $this->value = old($name, $default);
24+
return $this->value = old($inputName, $default);
2325
}
2426

2527
if ($bind !== false) {
@@ -30,6 +32,6 @@ private function setValue(
3032
$default = $bind->getTranslation($name, $language, false) ?: $default;
3133
}
3234

33-
$this->value = old("{$name}.{$language}", $default);
35+
$this->value = old("{$inputName}.{$language}", $default);
3436
}
3537
}

tests/Feature/BindTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ public function it_overrides_the_bound_target_with_the_old_request_data()
5151
->seeElement('input[name="radio"]:checked');
5252
}
5353

54+
/** @test */
55+
public function it_handles_old_nested_data()
56+
{
57+
$this->registerTestRoute('nested-validation-errors', function (Request $request) {
58+
$request->validate([
59+
'input.nested' => 'required',
60+
'textarea.nested' => 'required',
61+
'select.nested' => 'required',
62+
'checkbox.nested' => 'required',
63+
'radio.nested' => 'required',
64+
]);
65+
});
66+
67+
$this->visit('/nested-validation-errors')
68+
->type('d', 'input[nested]')
69+
->type('e', 'textarea[nested]')
70+
->select('f', 'select[nested]')
71+
->uncheck('checkbox[nested]')
72+
->check('radio[nested]')
73+
->press('Submit')
74+
->seeElement('input[name="input[nested]"][value="d"]')
75+
->seeInElement('textarea[name="textarea[nested]"]', 'e')
76+
->seeElement('option[value="f"]:selected')
77+
->seeElement('input[name="checkbox[nested]"]')
78+
->dontSeeElement('input[name="checkbox[nested]"]:checked')
79+
->seeElement('input[name="radio[nested]"]:checked');
80+
}
81+
5482
/** @test */
5583
public function it_overrides_the_default_value()
5684
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<x-form>
2+
<x-form-input name="input[nested]" />
3+
<x-form-textarea name="textarea[nested]" />
4+
<x-form-select name="select[nested]" :options="['c' => 'c', 'f' => 'f']" />
5+
<x-form-checkbox name="checkbox[nested]" />
6+
7+
<x-form-group name="radio[nested]">
8+
<x-form-radio name="radio[nested]" />
9+
</x-form-group>
10+
11+
<x-form-submit />
12+
</x-form>

0 commit comments

Comments
 (0)