Skip to content

Commit

Permalink
Don't repopulate select unless any errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bjuppa committed May 6, 2019
1 parent f600a4b commit f824a68
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion resources/views/forms/groups/select.blade.php
Original file line number Diff line number Diff line change
@@ -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', [
Expand Down
15 changes: 14 additions & 1 deletion tests/Feature/FormViewTests/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function test_optgroups()
$this->assertRegExp('/<select[\S\s]*>\s*<option\s*value="a"\s*>A<\/option>\s*<option\s*value="b"\s*>B<\/option>\s*<optgroup\s*label="A Group">\s*<option\s*value="c"\s*selected\s*>C<\/option>\s*<option\s*value="d"\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());
Expand All @@ -91,6 +91,19 @@ public function test_old_value_is_used_if_in_session()
'errors' => new MessageBag,
])->render();

$this->assertNotRegExp('/<option[\S\s]*value="a"[\S\s]*selected[\S\s]*>/', $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('/<option[\S\s]*value="a"[\S\s]*selected[\S\s]*>/', $output);
}

Expand Down

0 comments on commit f824a68

Please sign in to comment.