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