@@ -395,6 +395,116 @@ public function testResolveRequestRedirectsForWildcardAcceptHeader(): void
395395 $ this ->assertSame (303 , $ response ->getStatusCode ());
396396 }
397397
398+ public function testFailedValidationMayContinueToController (): void
399+ {
400+ service ('superglobals ' )->setPost ('title ' , ' Hello World ' );
401+
402+ $ formRequest = new class ($ this ->makeRequest ()) extends FormRequest {
403+ /**
404+ * @var array<string, string>
405+ */
406+ public array $ errors = [];
407+
408+ /**
409+ * @var array<string, mixed>
410+ */
411+ public array $ preparedData = [];
412+
413+ public function rules (): array
414+ {
415+ return [
416+ 'title ' => 'required ' ,
417+ 'body ' => 'required ' ,
418+ ];
419+ }
420+
421+ protected function prepareForValidation (array $ data ): array
422+ {
423+ $ data ['title ' ] = trim ($ data ['title ' ] ?? '' );
424+
425+ return $ data ;
426+ }
427+
428+ protected function failedValidation (array $ errors , array $ preparedData ): ?ResponseInterface
429+ {
430+ $ this ->errors = $ errors ;
431+ $ this ->preparedData = $ preparedData ;
432+
433+ return null ;
434+ }
435+ };
436+
437+ $ response = $ formRequest ->resolveRequest ();
438+
439+ $ this ->assertNull ($ response );
440+ $ this ->assertArrayHasKey ('body ' , $ formRequest ->errors );
441+ $ this ->assertSame (['title ' => 'Hello World ' ], $ formRequest ->preparedData );
442+ $ this ->assertSame ([], $ formRequest ->getValidated ());
443+ }
444+
445+ public function testNullableFailedValidationFormRequestPassesWithValidData (): void
446+ {
447+ service ('superglobals ' )->setPost ('title ' , ' Hello World ' );
448+ service ('superglobals ' )->setPost ('body ' , 'Some body text ' );
449+
450+ $ formRequest = new class ($ this ->makeRequest ()) extends FormRequest {
451+ public bool $ failedValidationCalled = false ;
452+
453+ public function rules (): array
454+ {
455+ return [
456+ 'title ' => 'required ' ,
457+ 'body ' => 'required ' ,
458+ ];
459+ }
460+
461+ protected function prepareForValidation (array $ data ): array
462+ {
463+ $ data ['title ' ] = trim ($ data ['title ' ] ?? '' );
464+
465+ return $ data ;
466+ }
467+
468+ protected function failedValidation (array $ errors , array $ preparedData ): ?ResponseInterface
469+ {
470+ $ this ->failedValidationCalled = true ;
471+
472+ return null ;
473+ }
474+ };
475+
476+ $ response = $ formRequest ->resolveRequest ();
477+
478+ $ this ->assertNull ($ response );
479+ $ this ->assertFalse ($ formRequest ->failedValidationCalled );
480+ $ this ->assertSame (['title ' => 'Hello World ' , 'body ' => 'Some body text ' ], $ formRequest ->getValidated ());
481+ }
482+
483+ public function testFailedValidationReturningResponseStillShortCircuits (): void
484+ {
485+ $ formRequest = new class ($ this ->makeRequest ()) extends FormRequest {
486+ public static bool $ called = false ;
487+
488+ public function rules (): array
489+ {
490+ return ['title ' => 'required ' ];
491+ }
492+
493+ protected function failedValidation (array $ errors , array $ preparedData ): ResponseInterface
494+ {
495+ self ::$ called = true ;
496+
497+ return service ('response ' )->setStatusCode (422 );
498+ }
499+ };
500+
501+ $ response = $ formRequest ->resolveRequest ();
502+
503+ $ this ->assertInstanceOf (ResponseInterface::class, $ response );
504+ $ this ->assertSame (422 , $ response ->getStatusCode ());
505+ $ this ->assertTrue ($ formRequest ::$ called );
506+ }
507+
398508 public function testPreparedValidationDataIsPassedToFailedValidationWithoutPreparingAgain (): void
399509 {
400510 service ('superglobals ' )->setPost ('title ' , ' Hello World ' );
@@ -590,6 +700,36 @@ protected function prepareForValidation(array $data): array
590700 $ this ->assertSame (['authorize ' ], $ formRequest ::$ order );
591701 }
592702
703+ public function testUnauthorizedRequestStopsBeforeNullableFailedValidation (): void
704+ {
705+ $ formRequest = new class ($ this ->makeRequest ()) extends FormRequest {
706+ public bool $ failedValidationCalled = false ;
707+
708+ public function rules (): array
709+ {
710+ return ['title ' => 'required ' ];
711+ }
712+
713+ public function isAuthorized (): bool
714+ {
715+ return false ;
716+ }
717+
718+ protected function failedValidation (array $ errors , array $ preparedData ): ?ResponseInterface
719+ {
720+ $ this ->failedValidationCalled = true ;
721+
722+ return null ;
723+ }
724+ };
725+
726+ $ response = $ formRequest ->resolveRequest ();
727+
728+ $ this ->assertInstanceOf (ResponseInterface::class, $ response );
729+ $ this ->assertSame (403 , $ response ->getStatusCode ());
730+ $ this ->assertFalse ($ formRequest ->failedValidationCalled );
731+ }
732+
593733 // -------------------------------------------------------------------------
594734 // Custom overrides
595735 // -------------------------------------------------------------------------
@@ -895,6 +1035,25 @@ public function testInvalidFormRequestRedirectsForAjaxRequest(): void
8951035 $ this ->assertSame (303 , $ response ->getStatusCode ());
8961036 }
8971037
1038+ // -------------------------------------------------------------------------
1039+ // Integration: controller-handled validation failure
1040+ // -------------------------------------------------------------------------
1041+
1042+ #[RunInSeparateProcess]
1043+ public function testContinuingInvalidFormRequestReachesController (): void
1044+ {
1045+ service ('superglobals ' )->setPost ('title ' , ' Draft ' );
1046+
1047+ $ response = $ this ->runRequest ('/posts/continuing ' , 'storeContinuing ' , 'POST ' );
1048+
1049+ $ this ->assertSame (422 , $ response ->getStatusCode ());
1050+
1051+ $ body = json_decode ((string ) $ response ->getBody (), true );
1052+ $ this ->assertArrayHasKey ('body ' , $ body ['errors ' ]);
1053+ $ this ->assertSame (['title ' => 'Draft ' ], $ body ['form ' ]);
1054+ $ this ->assertSame ([], $ body ['validated ' ]);
1055+ }
1056+
8981057 // -------------------------------------------------------------------------
8991058 // Integration: authorization failure
9001059 // -------------------------------------------------------------------------
0 commit comments