Skip to content

Commit e92187e

Browse files
committed
feat(http): expose prepared FormRequest data during validation failure
- Store prepared validation data before running FormRequest validation - Allow failedValidation() overrides to reuse the exact prepared payload - Avoid calling prepareForValidation() a second time in custom failure responses - Document normalized old-input handling Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 2ef1571 commit e92187e

4 files changed

Lines changed: 83 additions & 9 deletions

File tree

‎system/HTTP/FormRequest.php‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ abstract class FormRequest
3535
*/
3636
private array $validatedData = [];
3737

38+
/**
39+
* Data after prepareForValidation() and before validation rules run.
40+
*
41+
* @var array<string, mixed>
42+
*/
43+
private array $preparedValidationData = [];
44+
3845
/**
3946
* When called by the framework, the current IncomingRequest is injected
4047
* explicitly. When instantiated manually (e.g. in tests), the constructor
@@ -143,6 +150,21 @@ protected function prepareForValidation(array $data): array
143150
return $data;
144151
}
145152

153+
/**
154+
* Returns the data after prepareForValidation() has run.
155+
*
156+
* This is useful in failedValidation() when a custom failure response needs
157+
* the same prepared data that was passed to validation. This data has not
158+
* passed validation; use getValidated() or getValidatedInput() after
159+
* successful validation for trusted values.
160+
*
161+
* @return array<string, mixed>
162+
*/
163+
protected function getPreparedValidationData(): array
164+
{
165+
return $this->preparedValidationData;
166+
}
167+
146168
/**
147169
* Called when validation fails. Override to customize the failure response.
148170
*
@@ -249,16 +271,19 @@ protected function validationData(): array
249271
*/
250272
final public function resolveRequest(): ?ResponseInterface
251273
{
274+
$this->validatedData = [];
275+
$this->preparedValidationData = [];
276+
252277
if (! $this->isAuthorized()) {
253278
return $this->failedAuthorization();
254279
}
255280

256-
$data = $this->prepareForValidation($this->validationData());
281+
$this->preparedValidationData = $this->prepareForValidation($this->validationData());
257282

258283
$validation = service('validation')
259284
->setRules($this->rules(), $this->messages());
260285

261-
if (! $validation->run($data)) {
286+
if (! $validation->run($this->preparedValidationData)) {
262287
return $this->failedValidation($validation->getErrors());
263288
}
264289

‎tests/system/HTTP/FormRequestTest.php‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,48 @@ public function testResolveRequestRedirectsForWildcardAcceptHeader(): void
395395
$this->assertSame(303, $response->getStatusCode());
396396
}
397397

398+
public function testPreparedValidationDataIsAvailableDuringFailedValidationWithoutPreparingAgain(): void
399+
{
400+
service('superglobals')->setPost('title', ' Hello World ');
401+
402+
$formRequest = new class ($this->makeRequest()) extends FormRequest {
403+
public int $prepareCount = 0;
404+
405+
/**
406+
* @var array<string, mixed>
407+
*/
408+
public array $preparedData = [];
409+
410+
public function rules(): array
411+
{
412+
return [
413+
'title' => 'required',
414+
'body' => 'required',
415+
];
416+
}
417+
418+
protected function prepareForValidation(array $data): array
419+
{
420+
$this->prepareCount++;
421+
$data['title'] = trim($data['title'] ?? '');
422+
423+
return $data;
424+
}
425+
426+
protected function failedValidation(array $errors): ResponseInterface
427+
{
428+
$this->preparedData = $this->getPreparedValidationData();
429+
430+
return service('response')->setStatusCode(422)->setJSON(['errors' => $errors]);
431+
}
432+
};
433+
434+
$formRequest->resolveRequest();
435+
436+
$this->assertSame(1, $formRequest->prepareCount);
437+
$this->assertSame(['title' => 'Hello World'], $formRequest->preparedData);
438+
}
439+
398440
// -------------------------------------------------------------------------
399441
// Authorization failure
400442
// -------------------------------------------------------------------------

‎user_guide_src/source/incoming/form_requests.rst‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,20 @@ Flashing Normalized Input
226226

227227
If your ``prepareForValidation()`` transforms visible form fields (for example,
228228
trimming strings or canonicalizing values), ``old()`` will return the original
229-
submitted input because the redirect flashes the raw superglobals. To make
230-
``old()`` reflect the normalized values instead, override ``failedValidation()``
231-
and flash the normalized payload manually:
229+
submitted input because the redirect flashes the raw superglobals.
230+
231+
To make ``old()`` reflect the normalized values instead, override
232+
``failedValidation()`` and flash the data returned from
233+
``prepareForValidation()``:
232234

233235
.. literalinclude:: form_requests/013.php
234236
:lines: 2-
235237

238+
Use ``getPreparedValidationData()`` inside ``failedValidation()`` to read that
239+
prepared data without running ``prepareForValidation()`` again. The prepared
240+
data has not passed validation. After successful validation, use
241+
``getValidated()`` or ``getValidatedInput()`` for trusted values.
242+
236243
*****************************************
237244
How the Framework Resolves Form Requests
238245
*****************************************

‎user_guide_src/source/incoming/form_requests/013.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ protected function failedValidation(array $errors): ResponseInterface
3232
return service('response')->setStatusCode(422)->setJSON(['errors' => $errors]);
3333
}
3434

35-
// withInput() flashes the original superglobals and the validation
36-
// errors. We then overwrite old input with the normalized payload so
37-
// that old() returns the same values that were validated.
35+
// withInput() flashes the original superglobals and validation errors.
36+
// Then we overwrite old input with the prepared data so old() returns
37+
// the same values that were passed to validation.
3838
$redirect = redirect()->back()->withInput();
3939

4040
service('session')->setFlashdata('_ci_old_input', [
4141
'get' => [],
42-
'post' => $this->prepareForValidation($this->validationData()),
42+
'post' => $this->getPreparedValidationData(),
4343
]);
4444

4545
return $redirect;

0 commit comments

Comments
 (0)