From 7071cdbc1cac70ecaf1a86f597f446aefffa0663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 11:34:58 +0200 Subject: [PATCH 1/8] Don't repopulate input unless any errors --- resources/views/forms/elements/input.blade.php | 2 +- tests/Feature/FormViewTests/InputTest.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/resources/views/forms/elements/input.blade.php b/resources/views/forms/elements/input.blade.php index 3fe73657..8cc0aadd 100644 --- a/resources/views/forms/elements/input.blade.php +++ b/resources/views/forms/elements/input.blade.php @@ -1,6 +1,6 @@ \ No newline at end of file diff --git a/tests/Feature/FormViewTests/InputTest.php b/tests/Feature/FormViewTests/InputTest.php index 01b4be26..eeb829ad 100644 --- a/tests/Feature/FormViewTests/InputTest.php +++ b/tests/Feature/FormViewTests/InputTest.php @@ -87,7 +87,7 @@ public function test_default_value_is_empty_string() $this->assertRegExp('//', $output); } - public function test_old_value_is_used_if_in_session() + public function test_old_value_is_not_used_if_no_errors() { $this->withSession(['_old_input' => ['test' => 'old']]); request()->setLaravelSession(session()); @@ -96,6 +96,18 @@ public function test_old_value_is_used_if_in_session() 'errors' => new MessageBag, ])->render(); + $this->assertNotRegExp('//', $output); + } + + public function test_old_value_is_used_if_in_session_with_errors() + { + $this->withSession(['_old_input' => ['test' => 'old']]); + request()->setLaravelSession(session()); + $output = View::make('kontour::forms.input', [ + 'name' => 'test', + 'errors' => new MessageBag(['another_field' => ['An error']]), + ])->render(); + $this->assertRegExp('//', $output); } From a092deb912f1628ab5e0f8da382c924034c7af6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 11:40:44 +0200 Subject: [PATCH 2/8] Don't repopulate checkboxes unless any errors --- resources/views/forms/groups/checkboxes.blade.php | 2 +- tests/Feature/FormViewTests/CheckboxesTest.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/resources/views/forms/groups/checkboxes.blade.php b/resources/views/forms/groups/checkboxes.blade.php index cd6e1898..a13308bc 100644 --- a/resources/views/forms/groups/checkboxes.blade.php +++ b/resources/views/forms/groups/checkboxes.blade.php @@ -1,5 +1,5 @@
@include('kontour::forms.elements.label', [ diff --git a/tests/Feature/FormViewTests/CheckboxesTest.php b/tests/Feature/FormViewTests/CheckboxesTest.php index 65cb59a1..ff02d658 100644 --- a/tests/Feature/FormViewTests/CheckboxesTest.php +++ b/tests/Feature/FormViewTests/CheckboxesTest.php @@ -102,7 +102,7 @@ public function test_groups() $this->assertRegExp('/]*>[\S\s]*]*value="a"[^>]*>A[\S\s]*]*value="b"\s*checked[^>]*>B[\S\s]*]*>\s*A Group<\/legend>[\S\s]*]*value="c"\s*checked[^>]*>C[\S\s]*]*value="d"[^>]*>D[\S\s]*<\/fieldset>[\S\s]*<\/fieldset>/', $output); } - public function test_old_value_is_used_if_in_session() + public function test_old_value_is_not_used_if_no_errors() { $this->withSession(['_old_input' => ['test' => 'a']]); request()->setLaravelSession(session()); @@ -112,6 +112,19 @@ public function test_old_value_is_used_if_in_session() 'errors' => new MessageBag, ])->render(); + $this->assertNotRegExp('//', $output); + } + + public function test_old_value_is_used_if_in_session_with_errors() + { + $this->withSession(['_old_input' => ['test' => 'a']]); + request()->setLaravelSession(session()); + $output = View::make('kontour::forms.checkboxes', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag(['another_field' => ['An error']]), + ])->render(); + $this->assertRegExp('//', $output); } From 1fb9c89a62ecca45722e65204c45cfc366f0c3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 11:44:41 +0200 Subject: [PATCH 3/8] Don't repopulate checkbox unless any errors --- .../views/forms/elements/checkbox.blade.php | 2 +- tests/Feature/FormViewTests/CheckboxTest.php | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/resources/views/forms/elements/checkbox.blade.php b/resources/views/forms/elements/checkbox.blade.php index 242f4334..ff5fd2a6 100644 --- a/resources/views/forms/elements/checkbox.blade.php +++ b/resources/views/forms/elements/checkbox.blade.php @@ -1,5 +1,5 @@ any() ? old($name) : null) ?? $checked ?? $model[$name] ?? false)) checked @endif value="{{ $value ?? $checkboxDefaultValue ?? '1' }}" diff --git a/tests/Feature/FormViewTests/CheckboxTest.php b/tests/Feature/FormViewTests/CheckboxTest.php index 01d44b8d..3bb35e7b 100644 --- a/tests/Feature/FormViewTests/CheckboxTest.php +++ b/tests/Feature/FormViewTests/CheckboxTest.php @@ -96,7 +96,7 @@ public function test_value_can_be_set() $this->assertRegExp('//', $output); } - public function test_old_value_is_used_if_in_session() + public function test_old_value_is_not_used_if_no_errors() { $this->withSession(['_old_input' => ['test' => true]]); request()->setLaravelSession(session()); @@ -105,6 +105,18 @@ public function test_old_value_is_used_if_in_session() 'errors' => new MessageBag, ])->render(); + $this->assertNotRegExp('//', $output); + } + + public function test_old_value_is_used_if_in_session_with_errors() + { + $this->withSession(['_old_input' => ['test' => true]]); + request()->setLaravelSession(session()); + $output = View::make('kontour::forms.checkbox', [ + 'name' => 'test', + 'errors' => new MessageBag(['another_field' => ['An error']]), + ])->render(); + $this->assertRegExp('//', $output); } @@ -131,7 +143,7 @@ public function test_fallback_values_are_used_in_order() $value = $value['test']; } $regexp = '//'; - if($value) { + if ($value) { $this->assertRegExp($regexp, $output); } else { $this->assertNotRegExp($regexp, $output); From bb6ca45ab1e10670de9de3de0a513771a46f9f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 11:47:37 +0200 Subject: [PATCH 4/8] Don't repopulate multiselect unless any errors --- .../views/forms/groups/multiselect.blade.php | 2 +- tests/Feature/FormViewTests/MultiselectTest.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/resources/views/forms/groups/multiselect.blade.php b/resources/views/forms/groups/multiselect.blade.php index e9daeff3..ab40331d 100644 --- a/resources/views/forms/groups/multiselect.blade.php +++ b/resources/views/forms/groups/multiselect.blade.php @@ -1,5 +1,5 @@ <{{ $groupTag = $groupTag ?? 'div' }} - data-selected-options="{{ implode(' ', $selected = collect(old($name, $selected ?? $model[$name] ?? []))->map(function($item) { return strval($item instanceof Illuminate\Database\Eloquent\Model ? $item->getKey() : $item); })->all()) }}" + data-selected-options="{{ implode(' ', $selected = collect(($errors->any() ? old($name) : null) ?? $selected ?? $model[$name] ?? [])->map(function($item) { return strval($item instanceof Illuminate\Database\Eloquent\Model ? $item->getKey() : $item); })->all()) }}" @include('kontour::forms.partials.groupAttributes') > diff --git a/tests/Feature/FormViewTests/MultiselectTest.php b/tests/Feature/FormViewTests/MultiselectTest.php index 28a32928..30553b0d 100644 --- a/tests/Feature/FormViewTests/MultiselectTest.php +++ b/tests/Feature/FormViewTests/MultiselectTest.php @@ -117,7 +117,7 @@ public function test_optgroups() $this->assertRegExp('/\s*A<\/option>\s*B<\/option>\s*\s*C<\/option>\s*D<\/option>\s*<\/optgroup>\s*<\/select>/', $output); } - public function test_old_value_is_used_if_in_session() + public function test_old_value_is_not_used_if_no_errors() { $this->withSession(['_old_input' => ['test' => 'a']]); request()->setLaravelSession(session()); @@ -127,6 +127,19 @@ public function test_old_value_is_used_if_in_session() 'errors' => new MessageBag, ])->render(); + $this->assertNotRegExp('//', $output); + } + + public function test_old_value_is_used_if_in_session_with_errors() + { + $this->withSession(['_old_input' => ['test' => 'a']]); + request()->setLaravelSession(session()); + $output = View::make('kontour::forms.multiselect', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag(['another_field' => ['An error']]), + ])->render(); + $this->assertRegExp('//', $output); } From f600a4b2958b2a77f490d2bf1370a8f72ec48283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 11:50:07 +0200 Subject: [PATCH 5/8] Don't repopulate radiobuttons unless any errors --- .../views/forms/groups/radiobuttons.blade.php | 2 +- tests/Feature/FormViewTests/RadiobuttonsTest.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/resources/views/forms/groups/radiobuttons.blade.php b/resources/views/forms/groups/radiobuttons.blade.php index 11cacb5d..56424968 100644 --- a/resources/views/forms/groups/radiobuttons.blade.php +++ b/resources/views/forms/groups/radiobuttons.blade.php @@ -1,5 +1,5 @@
@include('kontour::forms.elements.label', [ diff --git a/tests/Feature/FormViewTests/RadiobuttonsTest.php b/tests/Feature/FormViewTests/RadiobuttonsTest.php index 1fb05ef3..b9777647 100644 --- a/tests/Feature/FormViewTests/RadiobuttonsTest.php +++ b/tests/Feature/FormViewTests/RadiobuttonsTest.php @@ -67,7 +67,7 @@ public function test_groups() $this->assertRegExp('/]*>[\S\s]*]*value="a"[^>]*>A[\S\s]*]*value="b"[^>]*>B[\S\s]*]*>\s*A Group<\/legend>[\S\s]*]*value="c"\s*checked[^>]*>C[\S\s]*]*value="d"[^>]*>D[\S\s]*<\/fieldset>[\S\s]*<\/fieldset>/', $output); } - public function test_old_value_is_used_if_in_session() + public function test_old_value_is_not_used_if_no_errors() { $this->withSession(['_old_input' => ['test' => 'a']]); request()->setLaravelSession(session()); @@ -77,6 +77,19 @@ public function test_old_value_is_used_if_in_session() 'errors' => new MessageBag, ])->render(); + $this->assertNotRegExp('//', $output); + } + + public function test_old_value_is_used_if_in_session_with_errors() + { + $this->withSession(['_old_input' => ['test' => 'a']]); + request()->setLaravelSession(session()); + $output = View::make('kontour::forms.radiobuttons', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag(['another_field' => ['An error']]), + ])->render(); + $this->assertRegExp('//', $output); } From f824a689ef3325b35fb44dd2c91824ef37866f90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 11:52:11 +0200 Subject: [PATCH 6/8] Don't repopulate select unless any errors --- resources/views/forms/groups/select.blade.php | 2 +- tests/Feature/FormViewTests/SelectTest.php | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/resources/views/forms/groups/select.blade.php b/resources/views/forms/groups/select.blade.php index 79bc116b..b8382eb4 100644 --- a/resources/views/forms/groups/select.blade.php +++ b/resources/views/forms/groups/select.blade.php @@ -1,5 +1,5 @@ <{{ $groupTag = $groupTag ?? 'div' }} - data-selected-option="{{ $selected = strval(old($name, $selected ?? $model[$name] ?? '')) }}" + data-selected-option="{{ $selected = strval(($errors->any() ? old($name) : null) ?? $selected ?? $model[$name] ?? '') }}" @include('kontour::forms.partials.groupAttributes') > @include('kontour::forms.label', [ diff --git a/tests/Feature/FormViewTests/SelectTest.php b/tests/Feature/FormViewTests/SelectTest.php index 7e43fe51..ff562f50 100644 --- a/tests/Feature/FormViewTests/SelectTest.php +++ b/tests/Feature/FormViewTests/SelectTest.php @@ -81,7 +81,7 @@ public function test_optgroups() $this->assertRegExp('/\s*A<\/option>\s*B<\/option>\s*\s*C<\/option>\s*D<\/option>\s*<\/optgroup>\s*<\/select>/', $output); } - public function test_old_value_is_used_if_in_session() + public function test_old_value_is_not_used_if_no_errors() { $this->withSession(['_old_input' => ['test' => 'a']]); request()->setLaravelSession(session()); @@ -91,6 +91,19 @@ public function test_old_value_is_used_if_in_session() 'errors' => new MessageBag, ])->render(); + $this->assertNotRegExp('//', $output); + } + + public function test_old_value_is_used_if_in_session_with_errors() + { + $this->withSession(['_old_input' => ['test' => 'a']]); + request()->setLaravelSession(session()); + $output = View::make('kontour::forms.select', [ + 'name' => 'test', + 'options' => ['a' => 'A', 'b' => 'B'], + 'errors' => new MessageBag(['another_field' => ['An error']]), + ])->render(); + $this->assertRegExp('//', $output); } From 178d8d902bfe58cdb739c8f3281c979524358096 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 11:55:10 +0200 Subject: [PATCH 7/8] Don't repopulate textarea unless any errors --- resources/views/forms/groups/textarea.blade.php | 2 +- tests/Feature/FormViewTests/TextareaTest.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/resources/views/forms/groups/textarea.blade.php b/resources/views/forms/groups/textarea.blade.php index 390208dc..dcf91e4a 100644 --- a/resources/views/forms/groups/textarea.blade.php +++ b/resources/views/forms/groups/textarea.blade.php @@ -10,7 +10,7 @@ @include('kontour::forms.partials.inputAttributes', [ 'errorsId' => $errorsId = $controlId . ($errorsSuffix ?? 'Errors'), ]) - >{{ old($name, $value ?? $slot ?? $model[$name] ?? '') }} + >{{ ($errors->any() ? old($name) : null) ?? $value ?? $slot ?? $model[$name] ?? '' }} {{ $afterControl ?? '' }} @include('kontour::forms.partials.errors') diff --git a/tests/Feature/FormViewTests/TextareaTest.php b/tests/Feature/FormViewTests/TextareaTest.php index 0cb08dcb..eb890449 100644 --- a/tests/Feature/FormViewTests/TextareaTest.php +++ b/tests/Feature/FormViewTests/TextareaTest.php @@ -53,7 +53,7 @@ public function test_default_value_is_empty_string() $this->assertRegExp('/<\/textarea>/', $output); } - public function test_old_value_is_used_if_in_session() + public function test_old_value_is_not_used_if_no_errors() { $this->withSession(['_old_input' => ['test' => 'old']]); request()->setLaravelSession(session()); @@ -62,6 +62,18 @@ public function test_old_value_is_used_if_in_session() 'errors' => new MessageBag, ])->render(); + $this->assertNotRegExp('/old<\/textarea>/', $output); + } + + public function test_old_value_is_used_if_in_session_with_errors() + { + $this->withSession(['_old_input' => ['test' => 'old']]); + request()->setLaravelSession(session()); + $output = View::make('kontour::forms.textarea', [ + 'name' => 'test', + 'errors' => new MessageBag(['another_field' => ['An error']]), + ])->render(); + $this->assertRegExp('/old<\/textarea>/', $output); } From bc7111828e5a2cf9e0b4f8534298adc3dad92b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Nilsved?= Date: Mon, 6 May 2019 12:04:24 +0200 Subject: [PATCH 8/8] Document repopulation of form data --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index ea9a77a6..1cef019b 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,10 @@ This is actually a good pattern for scoping variables to one of the forms on you @include('my_form_partial', ['errors' => $errors->my_form_bag, 'model' => $user]) ``` +If the `$errors` bag contains any errors, +[old input data from the previous request](https://laravel.com/docs/5.8/helpers#method-old) +will be used to repopulate the form. + The `id` attribute is set automatically on created elements that need it, and it's usually derieved from the `$name` variable. If you get an id conflict on a page where two inputs may have the same name,