Skip to content

Commit 34fbfe6

Browse files
andjschjasonvargaclaude
authored
[6.x] Fill old input that wasn't flashed by a form submission (#16028)
Co-authored-by: Jason Varga <jason@pixelfear.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 640e113 commit 34fbfe6

5 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎src/Forms/JsDrivers/AbstractJsDriver.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Collection;
66
use Statamic\Contracts\Forms\Submission;
77
use Statamic\Forms\Form;
8+
use Statamic\Forms\OldInput;
89
use Statamic\Support\Arr;
910
use Statamic\Support\Str;
1011
use Statamic\Tags\Parameters;
@@ -152,7 +153,7 @@ protected function getInitialFormData(): array
152153
->fields()
153154
->addValues([
154155
...$values,
155-
...(session('errors')?->hasBag('form.'.$this->form->handle()) ? (old() ?? []) : []),
156+
...(OldInput::shouldFill('form.'.$this->form->handle()) ? (old() ?? []) : []),
156157
])
157158
->preProcess()
158159
->values()

‎src/Forms/OldInput.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Statamic\Forms;
4+
5+
use Statamic\Support\Str;
6+
7+
/** @internal */
8+
class OldInput
9+
{
10+
/**
11+
* Whether the old (flashed) input should be filled into a form's fields.
12+
*
13+
* Old input is flashed globally by Laravel and is not scoped to a form,
14+
* so it is attributed using the flashed error bags: a `form.{handle}` bag
15+
* is only ever flashed by that form's failed submission. Fill the old
16+
* input into every form, unless it can be attributed to a different
17+
* form's failed submission.
18+
*/
19+
public static function shouldFill(string $errorBag): bool
20+
{
21+
$formBags = collect(session('errors')?->getBags() ?? [])
22+
->keys()
23+
->filter(fn ($bag) => Str::startsWith($bag, 'form.'));
24+
25+
return $formBags->isEmpty() || $formBags->contains($errorBag);
26+
}
27+
}

‎src/Tags/Concerns/RendersForms.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\MessageBag;
88
use Statamic\Fields\Field;
99
use Statamic\Forms\Fields\FormField;
10+
use Statamic\Forms\OldInput;
1011
use Statamic\Forms\RenderableField;
1112
use Statamic\Support\Arr;
1213
use Statamic\Support\Str;
@@ -136,7 +137,7 @@ protected function getRenderableField($field, $errorBag = 'default', $manipulate
136137
$errors = session('errors') ? session('errors')->getBag($errorBag) : new MessageBag;
137138

138139
$missing = Str::random();
139-
$hasOld = ! $field->form() || $errors->any();
140+
$hasOld = ! $field->form() || OldInput::shouldFill($errorBag);
140141
$old = $hasOld ? old($field->handle(), $missing) : $missing;
141142
$default = $field->value() ?? $field->defaultValue();
142143
$value = $old === $missing ? $default : $old;

‎tests/Tags/Form/FormCreateAlpineTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ public function it_does_not_render_old_data_in_x_data_for_a_different_form()
198198
$this->assertStringContainsString('&quot;name&quot;:null', $output);
199199
}
200200

201+
#[Test]
202+
public function it_renders_old_data_in_x_data_flashed_without_form_errors()
203+
{
204+
// Simulates old input flashed by something other than a form submission,
205+
// for example a failed login or a custom controller redirecting with input.
206+
$this->withSession(['_old_input' => ['name' => 'Frodo Braggins']]);
207+
request()->setLaravelSession(app('session.store'));
208+
209+
$output = $this->tag('{{ form:contact js="alpine" }}{{ /form:contact }}');
210+
211+
$this->assertStringContainsString('&quot;name&quot;:&quot;Frodo Braggins&quot;', $output);
212+
}
213+
201214
#[Test]
202215
public function it_renders_scoped_x_data_on_form_tag()
203216
{

‎tests/Tags/Form/FormCreateTest.php‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Illuminate\Support\Facades\Bus;
99
use Illuminate\Support\Facades\Event;
1010
use Illuminate\Support\Facades\Storage;
11+
use Illuminate\Support\MessageBag;
12+
use Illuminate\Support\ViewErrorBag;
1113
use Illuminate\Validation\ValidationException;
1214
use PHPUnit\Framework\Attributes\DataProvider;
1315
use PHPUnit\Framework\Attributes\Test;
@@ -1386,6 +1388,51 @@ public function it_only_renders_old_input_for_the_submitted_form()
13861388

13871389
$output = $this->tag('{{ form:newsletter }}{{ fields }}[{{ handle }}={{ value }}]{{ /fields }}{{ /form:newsletter }}');
13881390
$this->assertStringContainsString('[email=]', $output);
1391+
1392+
$output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:contact }}');
1393+
$this->assertStringContainsString('[email=not-an-email]', $output);
1394+
1395+
$output = $this->tag('{{ form:newsletter }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:newsletter }}');
1396+
$this->assertStringContainsString('[email=null]', $output);
1397+
}
1398+
1399+
#[Test]
1400+
public function it_renders_old_input_flashed_without_form_errors()
1401+
{
1402+
// Simulates old input flashed by something other than a form submission,
1403+
// for example a failed login or a custom controller redirecting with input.
1404+
$this->withSession(['_old_input' => ['name' => 'Frodo Braggins']]);
1405+
request()->setLaravelSession(app('session.store'));
1406+
1407+
$output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:contact }}');
1408+
1409+
$this->assertStringContainsString('[name=Frodo Braggins]', $output);
1410+
$this->assertStringContainsString('[email=null]', $output);
1411+
$this->assertStringContainsString('[message=null]', $output);
1412+
1413+
$output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ value }}]{{ /fields }}{{ /form:contact }}');
1414+
1415+
$this->assertStringContainsString('[name=Frodo Braggins]', $output);
1416+
$this->assertStringContainsString('[email=]', $output);
1417+
}
1418+
1419+
#[Test]
1420+
public function it_renders_old_input_flashed_with_a_non_form_error_bag()
1421+
{
1422+
// Simulates old input flashed by something other than a form submission,
1423+
// while a non-form error bag (e.g. from a failed login) is also in the session.
1424+
$this->withSession([
1425+
'_old_input' => ['name' => 'Frodo Braggins', 'email' => 'flashed@example.com'],
1426+
'errors' => (new ViewErrorBag)->put('default', new MessageBag(['email' => 'Invalid.'])),
1427+
]);
1428+
request()->setLaravelSession(app('session.store'));
1429+
1430+
$output = $this->tag('{{ form:contact }}{{ fields }}[{{ handle }}={{ old ?? "null" }}]{{ /fields }}{{ /form:contact }}');
1431+
1432+
// The old input can't be attributed to another form submission, so it's rendered.
1433+
$this->assertStringContainsString('[name=Frodo Braggins]', $output);
1434+
$this->assertStringContainsString('[email=flashed@example.com]', $output);
1435+
$this->assertStringContainsString('[message=null]', $output);
13891436
}
13901437

13911438
#[Test]

0 commit comments

Comments
 (0)