Skip to content

Commit

Permalink
Don't repopulate textarea unless any errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bjuppa committed May 6, 2019
1 parent f824a68 commit 178d8d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion resources/views/forms/groups/textarea.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@include('kontour::forms.partials.inputAttributes', [
'errorsId' => $errorsId = $controlId . ($errorsSuffix ?? 'Errors'),
])
>{{ old($name, $value ?? $slot ?? $model[$name] ?? '') }}</textarea>
>{{ ($errors->any() ? old($name) : null) ?? $value ?? $slot ?? $model[$name] ?? '' }}</textarea>
{{ $afterControl ?? '' }}
@include('kontour::forms.partials.errors')
</div>
Expand Down
14 changes: 13 additions & 1 deletion tests/Feature/FormViewTests/TextareaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function test_default_value_is_empty_string()
$this->assertRegExp('/<textarea[\S\s]*><\/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());
Expand All @@ -62,6 +62,18 @@ public function test_old_value_is_used_if_in_session()
'errors' => new MessageBag,
])->render();

$this->assertNotRegExp('/<textarea[\S\s]*>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('/<textarea[\S\s]*>old<\/textarea>/', $output);
}

Expand Down

0 comments on commit 178d8d9

Please sign in to comment.