Skip to content

Commit 1c39210

Browse files
committed
fix: preserve old input source during FormRequest failure
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent d150c2f commit 1c39210

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎system/HTTP/FormRequest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ protected function failedValidation(array $errors, array $preparedData): Respons
168168
: 'post';
169169

170170
$oldInput = [
171-
'get' => [],
172-
'post' => [],
171+
'get' => service('superglobals')->getGetArray(),
172+
'post' => service('superglobals')->getPostArray(),
173173
];
174174
$oldInput[$key] = $preparedData;
175175

‎tests/system/HTTP/FormRequestTest.php‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,38 @@ protected function prepareForValidation(array $data): array
499499
$this->assertSame([], $_SESSION['_ci_old_input']['post']);
500500
}
501501

502+
#[RunInSeparateProcess]
503+
public function testDefaultFailedValidationPreservesGetDataWhenPostDataIsPrepared(): void
504+
{
505+
/** @var array<string, mixed> $_SESSION */
506+
$_SESSION = [];
507+
508+
service('superglobals')->setGet('category', '2');
509+
service('superglobals')->setPost('title', ' Hello World ');
510+
511+
$formRequest = new class ($this->makeRequest()) extends FormRequest {
512+
public function rules(): array
513+
{
514+
return [
515+
'title' => 'required',
516+
'body' => 'required',
517+
];
518+
}
519+
520+
protected function prepareForValidation(array $data): array
521+
{
522+
$data['title'] = trim($data['title'] ?? '');
523+
524+
return $data;
525+
}
526+
};
527+
528+
$formRequest->resolveRequest();
529+
530+
$this->assertSame(['category' => '2'], $_SESSION['_ci_old_input']['get']);
531+
$this->assertSame(['title' => 'Hello World'], $_SESSION['_ci_old_input']['post']);
532+
}
533+
502534
// -------------------------------------------------------------------------
503535
// Authorization failure
504536
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)