From c0eb477bddd3b8cdad1a3d3b2995fdcb03237e52 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Mon, 15 Feb 2021 16:52:42 +0100 Subject: [PATCH] Bugfix for 'old' nested data (#49) --- CHANGELOG.md | 4 +++ src/Components/Component.php | 11 ++++++++ src/Components/FormCheckbox.php | 2 +- src/Components/FormErrors.php | 2 +- src/Components/FormRadio.php | 6 ++-- src/Components/FormSelect.php | 2 +- src/Components/HandlesDefaultAndOldValue.php | 6 ++-- tests/Feature/BindTest.php | 28 +++++++++++++++++++ .../views/nested-validation-errors.blade.php | 12 ++++++++ 9 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 tests/Feature/views/nested-validation-errors.blade.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 606b08b..1d1a75e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-form-components` will be documented in this file +## 2.5.4 - 2020-02-15 + +- Bugfix for old nested data + ## 2.5.3 - 2020-02-11 - Bugfix for setting radio elements as checked/default diff --git a/src/Components/Component.php b/src/Components/Component.php index bc39ec7..c62df1a 100644 --- a/src/Components/Component.php +++ b/src/Components/Component.php @@ -88,4 +88,15 @@ protected function generateIdByName(): string { return "auto_id_" . $this->name; } + + /** + * Converts a bracket-notation to a dotted-notation + * + * @param string $name + * @return string + */ + protected static function convertBracketsToDots($name): string + { + return str_replace(['[', ']'], ['.', ''], $name); + } } diff --git a/src/Components/FormCheckbox.php b/src/Components/FormCheckbox.php index d1fb19c..f04829f 100644 --- a/src/Components/FormCheckbox.php +++ b/src/Components/FormCheckbox.php @@ -35,7 +35,7 @@ public function __construct( $inputName = Str::before($name, '[]'); - if ($oldData = old($inputName)) { + if ($oldData = old(static::convertBracketsToDots($inputName))) { $this->checked = in_array($value, Arr::wrap($oldData)); } diff --git a/src/Components/FormErrors.php b/src/Components/FormErrors.php index 56b803d..afcc2cb 100644 --- a/src/Components/FormErrors.php +++ b/src/Components/FormErrors.php @@ -16,7 +16,7 @@ class FormErrors extends Component */ public function __construct(string $name, string $bag = 'default') { - $this->name = str_replace(['[', ']'], ['.', ''], Str::before($name, '[]')); + $this->name = static::convertBracketsToDots(Str::before($name, '[]')); $this->bag = $bag; } diff --git a/src/Components/FormRadio.php b/src/Components/FormRadio.php index cf53e5e..0c96f45 100644 --- a/src/Components/FormRadio.php +++ b/src/Components/FormRadio.php @@ -25,8 +25,10 @@ public function __construct( $this->value = $value; $this->showErrors = $showErrors; - if (old($name)) { - $this->checked = old($name) == $value; + $inputName = static::convertBracketsToDots($name); + + if (old($inputName)) { + $this->checked = old($inputName) == $value; } if (!session()->hasOldInput() && $this->isNotWired()) { diff --git a/src/Components/FormSelect.php b/src/Components/FormSelect.php index 0bd6282..df2dccb 100644 --- a/src/Components/FormSelect.php +++ b/src/Components/FormSelect.php @@ -42,7 +42,7 @@ public function __construct( $default = $this->getBoundValue($bind, $inputName) ?: $default; - $this->selectedKey = old($inputName, $default); + $this->selectedKey = old(static::convertBracketsToDots($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 f61ad59..6980477 100644 --- a/src/Components/HandlesDefaultAndOldValue.php +++ b/src/Components/HandlesDefaultAndOldValue.php @@ -16,10 +16,12 @@ private function setValue( return; } + $inputName = static::convertBracketsToDots($name); + if (!$language) { $default = $this->getBoundValue($bind, $name) ?: $default; - return $this->value = old($name, $default); + return $this->value = old($inputName, $default); } if ($bind !== false) { @@ -30,6 +32,6 @@ private function setValue( $default = $bind->getTranslation($name, $language, false) ?: $default; } - $this->value = old("{$name}.{$language}", $default); + $this->value = old("{$inputName}.{$language}", $default); } } diff --git a/tests/Feature/BindTest.php b/tests/Feature/BindTest.php index 5207b7b..858d3de 100644 --- a/tests/Feature/BindTest.php +++ b/tests/Feature/BindTest.php @@ -51,6 +51,34 @@ public function it_overrides_the_bound_target_with_the_old_request_data() ->seeElement('input[name="radio"]:checked'); } + /** @test */ + public function it_handles_old_nested_data() + { + $this->registerTestRoute('nested-validation-errors', function (Request $request) { + $request->validate([ + 'input.nested' => 'required', + 'textarea.nested' => 'required', + 'select.nested' => 'required', + 'checkbox.nested' => 'required', + 'radio.nested' => 'required', + ]); + }); + + $this->visit('/nested-validation-errors') + ->type('d', 'input[nested]') + ->type('e', 'textarea[nested]') + ->select('f', 'select[nested]') + ->uncheck('checkbox[nested]') + ->check('radio[nested]') + ->press('Submit') + ->seeElement('input[name="input[nested]"][value="d"]') + ->seeInElement('textarea[name="textarea[nested]"]', 'e') + ->seeElement('option[value="f"]:selected') + ->seeElement('input[name="checkbox[nested]"]') + ->dontSeeElement('input[name="checkbox[nested]"]:checked') + ->seeElement('input[name="radio[nested]"]:checked'); + } + /** @test */ public function it_overrides_the_default_value() { diff --git a/tests/Feature/views/nested-validation-errors.blade.php b/tests/Feature/views/nested-validation-errors.blade.php new file mode 100644 index 0000000..0f658be --- /dev/null +++ b/tests/Feature/views/nested-validation-errors.blade.php @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file