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] 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); }