Skip to content

Commit

Permalink
Merge pull request #127 from kontenta/feature/only-use-old-in-forms-w…
Browse files Browse the repository at this point in the history
…hen-errors

Only use old data in forms when errors are present
  • Loading branch information
erik-epineer authored May 13, 2019
2 parents 30e3b0e + c3db055 commit de5b487
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 15 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion resources/views/forms/elements/checkbox.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<input type="checkbox"
@if(old($name, $checked ?? $model[$name] ?? false))
@if(($errors->any() ? old($name) : null) ?? $checked ?? $model[$name] ?? false))
checked
@endif
value="{{ $value ?? $checkboxDefaultValue ?? '1' }}"
Expand Down
2 changes: 1 addition & 1 deletion resources/views/forms/elements/input.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<input type="{{ $type = $type ?? 'text' }}"
@if($type != 'password')
value="{{ old($name, $value ?? $slot ?? $model[$name] ?? '') }}"
value="{{ ($errors->any() ? old($name) : null) ?? $value ?? $slot ?? $model[$name] ?? '' }}"
@endif
@include('kontour::forms.partials.inputAttributes')
>
2 changes: 1 addition & 1 deletion resources/views/forms/groups/checkboxes.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<fieldset id="{{ $groupId = $controlId ?? (($idPrefix ?? '') . $name) }}"
data-checked-checkboxes="{{ implode(' ', $selected = collect(old($name, $selected ?? $model[$name] ?? []))->map(function($item) { return strval($item instanceof Illuminate\Database\Eloquent\Model ? $item->getKey() : $item); })->all()) }}"
data-checked-checkboxes="{{ 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')
>
@include('kontour::forms.elements.label', [
Expand Down
2 changes: 1 addition & 1 deletion resources/views/forms/groups/multiselect.blade.php
Original file line number Diff line number Diff line change
@@ -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')
>
<input type="hidden" name="{{ $name }}" value="">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/forms/groups/radiobuttons.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<fieldset id="{{ $groupId = $controlId ?? (($idPrefix ?? '') . $name) }}"
data-checked-radio="{{ $selected = strval(old($name, $selected ?? $model[$name] ?? '')) }}"
data-checked-radio="{{ $selected = strval(($errors->any() ? old($name) : null) ?? $selected ?? $model[$name] ?? '') }}"
@include('kontour::forms.partials.groupAttributes')
>
@include('kontour::forms.elements.label', [
Expand Down
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
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
16 changes: 14 additions & 2 deletions tests/Feature/FormViewTests/CheckboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function test_value_can_be_set()
$this->assertRegExp('/<input[\S\s]*value="3"[\S\s]*>/', $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());
Expand All @@ -105,6 +105,18 @@ public function test_old_value_is_used_if_in_session()
'errors' => new MessageBag,
])->render();

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

Expand All @@ -131,7 +143,7 @@ public function test_fallback_values_are_used_in_order()
$value = $value['test'];
}
$regexp = '/<input[\S\s]*checked[\S\s]*>/';
if($value) {
if ($value) {
$this->assertRegExp($regexp, $output);
} else {
$this->assertNotRegExp($regexp, $output);
Expand Down
15 changes: 14 additions & 1 deletion tests/Feature/FormViewTests/CheckboxesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function test_groups()
$this->assertRegExp('/<fieldset[^>]*>[\S\s]*<input[^>]*value="a"[^>]*>A[\S\s]*<input[^>]*value="b"\s*checked[^>]*>B[\S\s]*<fieldset[^>]*>\s*<legend>A Group<\/legend>[\S\s]*<input[^>]*value="c"\s*checked[^>]*>C[\S\s]*<input[^>]*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());
Expand All @@ -112,6 +112,19 @@ public function test_old_value_is_used_if_in_session()
'errors' => new MessageBag,
])->render();

$this->assertNotRegExp('/<input[\S\s]*value="a"[\S\s]*checked[\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.checkboxes', [
'name' => 'test',
'options' => ['a' => 'A', 'b' => 'B'],
'errors' => new MessageBag(['another_field' => ['An error']]),
])->render();

$this->assertRegExp('/<input[\S\s]*value="a"[\S\s]*checked[\S\s]*>/', $output);
}

Expand Down
14 changes: 13 additions & 1 deletion tests/Feature/FormViewTests/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function test_default_value_is_empty_string()
$this->assertRegExp('/<input[\S\s]*value=""[\S\s]*>/', $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 @@ -96,6 +96,18 @@ public function test_old_value_is_used_if_in_session()
'errors' => new MessageBag,
])->render();

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

Expand Down
15 changes: 14 additions & 1 deletion tests/Feature/FormViewTests/MultiselectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function test_optgroups()
$this->assertRegExp('/<select[\S\s]*>\s*<option\s*value="a"\s*>A<\/option>\s*<option\s*value="b"\s*selected\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 @@ -127,6 +127,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.multiselect', [
'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
15 changes: 14 additions & 1 deletion tests/Feature/FormViewTests/RadiobuttonsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function test_groups()
$this->assertRegExp('/<fieldset[^>]*>[\S\s]*<input[^>]*value="a"[^>]*>A[\S\s]*<input[^>]*value="b"[^>]*>B[\S\s]*<fieldset[^>]*>\s*<legend>A Group<\/legend>[\S\s]*<input[^>]*value="c"\s*checked[^>]*>C[\S\s]*<input[^>]*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());
Expand All @@ -77,6 +77,19 @@ public function test_old_value_is_used_if_in_session()
'errors' => new MessageBag,
])->render();

$this->assertNotRegExp('/<input[\S\s]*value="a"[\S\s]*checked[\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.radiobuttons', [
'name' => 'test',
'options' => ['a' => 'A', 'b' => 'B'],
'errors' => new MessageBag(['another_field' => ['An error']]),
])->render();

$this->assertRegExp('/<input[\S\s]*value="a"[\S\s]*checked[\S\s]*>/', $output);
}

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
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 de5b487

Please sign in to comment.