diff --git a/README.md b/README.md
index ea9a77a6..1cef019b 100644
--- a/README.md
+++ b/README.md
@@ -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,
diff --git a/resources/views/forms/elements/checkbox.blade.php b/resources/views/forms/elements/checkbox.blade.php
index 242f4334..ff5fd2a6 100644
--- a/resources/views/forms/elements/checkbox.blade.php
+++ b/resources/views/forms/elements/checkbox.blade.php
@@ -1,5 +1,5 @@
any() ? old($name) : null) ?? $checked ?? $model[$name] ?? false))
checked
@endif
value="{{ $value ?? $checkboxDefaultValue ?? '1' }}"
diff --git a/resources/views/forms/elements/input.blade.php b/resources/views/forms/elements/input.blade.php
index 3fe73657..8cc0aadd 100644
--- a/resources/views/forms/elements/input.blade.php
+++ b/resources/views/forms/elements/input.blade.php
@@ -1,6 +1,6 @@
\ No newline at end of file
diff --git a/resources/views/forms/groups/checkboxes.blade.php b/resources/views/forms/groups/checkboxes.blade.php
index cd6e1898..a13308bc 100644
--- a/resources/views/forms/groups/checkboxes.blade.php
+++ b/resources/views/forms/groups/checkboxes.blade.php
@@ -1,5 +1,5 @@
@include('kontour::forms.elements.label', [
diff --git a/resources/views/forms/groups/multiselect.blade.php b/resources/views/forms/groups/multiselect.blade.php
index e9daeff3..ab40331d 100644
--- a/resources/views/forms/groups/multiselect.blade.php
+++ b/resources/views/forms/groups/multiselect.blade.php
@@ -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')
>
diff --git a/resources/views/forms/groups/radiobuttons.blade.php b/resources/views/forms/groups/radiobuttons.blade.php
index 11cacb5d..56424968 100644
--- a/resources/views/forms/groups/radiobuttons.blade.php
+++ b/resources/views/forms/groups/radiobuttons.blade.php
@@ -1,5 +1,5 @@
@include('kontour::forms.elements.label', [
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/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/CheckboxTest.php b/tests/Feature/FormViewTests/CheckboxTest.php
index 01d44b8d..3bb35e7b 100644
--- a/tests/Feature/FormViewTests/CheckboxTest.php
+++ b/tests/Feature/FormViewTests/CheckboxTest.php
@@ -96,7 +96,7 @@ public function test_value_can_be_set()
$this->assertRegExp('/ /', $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());
@@ -105,6 +105,18 @@ 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' => true]]);
+ request()->setLaravelSession(session());
+ $output = View::make('kontour::forms.checkbox', [
+ 'name' => 'test',
+ 'errors' => new MessageBag(['another_field' => ['An error']]),
+ ])->render();
+
$this->assertRegExp('/ /', $output);
}
@@ -131,7 +143,7 @@ public function test_fallback_values_are_used_in_order()
$value = $value['test'];
}
$regexp = '/ /';
- if($value) {
+ if ($value) {
$this->assertRegExp($regexp, $output);
} else {
$this->assertNotRegExp($regexp, $output);
diff --git a/tests/Feature/FormViewTests/CheckboxesTest.php b/tests/Feature/FormViewTests/CheckboxesTest.php
index 65cb59a1..ff02d658 100644
--- a/tests/Feature/FormViewTests/CheckboxesTest.php
+++ b/tests/Feature/FormViewTests/CheckboxesTest.php
@@ -102,7 +102,7 @@ public function test_groups()
$this->assertRegExp('/]*>[\S\s]* ]*value="a"[^>]*>A[\S\s]* ]*value="b"\s*checked[^>]*>B[\S\s]*]*>\s*A Group<\/legend>[\S\s]* ]*value="c"\s*checked[^>]*>C[\S\s]* ]*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());
@@ -112,6 +112,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.checkboxes', [
+ 'name' => 'test',
+ 'options' => ['a' => 'A', 'b' => 'B'],
+ 'errors' => new MessageBag(['another_field' => ['An error']]),
+ ])->render();
+
$this->assertRegExp('/ /', $output);
}
diff --git a/tests/Feature/FormViewTests/InputTest.php b/tests/Feature/FormViewTests/InputTest.php
index 01b4be26..eeb829ad 100644
--- a/tests/Feature/FormViewTests/InputTest.php
+++ b/tests/Feature/FormViewTests/InputTest.php
@@ -87,7 +87,7 @@ public function test_default_value_is_empty_string()
$this->assertRegExp('/ /', $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());
@@ -96,6 +96,18 @@ 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' => 'old']]);
+ request()->setLaravelSession(session());
+ $output = View::make('kontour::forms.input', [
+ 'name' => 'test',
+ 'errors' => new MessageBag(['another_field' => ['An error']]),
+ ])->render();
+
$this->assertRegExp('/ /', $output);
}
diff --git a/tests/Feature/FormViewTests/MultiselectTest.php b/tests/Feature/FormViewTests/MultiselectTest.php
index 28a32928..30553b0d 100644
--- a/tests/Feature/FormViewTests/MultiselectTest.php
+++ b/tests/Feature/FormViewTests/MultiselectTest.php
@@ -117,7 +117,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());
@@ -127,6 +127,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.multiselect', [
+ 'name' => 'test',
+ 'options' => ['a' => 'A', 'b' => 'B'],
+ 'errors' => new MessageBag(['another_field' => ['An error']]),
+ ])->render();
+
$this->assertRegExp('/ /', $output);
}
diff --git a/tests/Feature/FormViewTests/RadiobuttonsTest.php b/tests/Feature/FormViewTests/RadiobuttonsTest.php
index 1fb05ef3..b9777647 100644
--- a/tests/Feature/FormViewTests/RadiobuttonsTest.php
+++ b/tests/Feature/FormViewTests/RadiobuttonsTest.php
@@ -67,7 +67,7 @@ public function test_groups()
$this->assertRegExp('/]*>[\S\s]* ]*value="a"[^>]*>A[\S\s]* ]*value="b"[^>]*>B[\S\s]*]*>\s*A Group<\/legend>[\S\s]* ]*value="c"\s*checked[^>]*>C[\S\s]* ]*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());
@@ -77,6 +77,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.radiobuttons', [
+ 'name' => 'test',
+ 'options' => ['a' => 'A', 'b' => 'B'],
+ 'errors' => new MessageBag(['another_field' => ['An error']]),
+ ])->render();
+
$this->assertRegExp('/ /', $output);
}
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);
}
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('/