diff --git a/src/Middleware.php b/src/Middleware.php index 81b6ab9d..db99b33c 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -21,6 +21,13 @@ class Middleware */ protected $rootView = 'app'; + /** + * Determines if validation errors should be mapped to a single error message per field. + * + * @var bool + */ + protected $withAllErrors = false; + /** * Determine the current asset version. * @@ -153,7 +160,7 @@ public function resolveValidationErrors(Request $request) return (object) collect($bags)->map(function ($bag) { return (object) collect($bag->messages())->map(function ($errors) { - return $errors[0]; + return $this->withAllErrors ? $errors : $errors[0]; })->toArray(); })->pipe(function ($bags) use ($request) { if ($bags->has('default') && $request->header(Header::ERROR_BAG)) { diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index fc143f75..98bd4fa8 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -15,6 +15,7 @@ use Inertia\Middleware; use Inertia\Tests\Stubs\CustomUrlResolverMiddleware; use Inertia\Tests\Stubs\ExampleMiddleware; +use Inertia\Tests\Stubs\WithAllErrorsMiddleware; use LogicException; use PHPUnit\Framework\Attributes\After; @@ -168,11 +169,11 @@ public function test_validation_errors_can_be_empty(): void $this->withoutExceptionHandling()->get('/'); } - public function test_validation_errors_are_returned_in_the_correct_format(): void + public function test_validation_errors_are_mapped_to_strings_by_default(): void { Session::put('errors', (new ViewErrorBag)->put('default', new MessageBag([ - 'name' => 'The name field is required.', - 'email' => 'Not a valid email address.', + 'name' => ['The name field is required.'], + 'email' => ['Not a valid email address.', 'Another email error.'], ]))); Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () { @@ -186,6 +187,27 @@ public function test_validation_errors_are_returned_in_the_correct_format(): voi $this->withoutExceptionHandling()->get('/'); } + public function test_validation_errors_can_remain_multiple_per_field(): void + { + Session::put('errors', (new ViewErrorBag)->put('default', new MessageBag([ + 'name' => ['The name field is required.'], + 'email' => ['Not a valid email address.', 'Another email error.'], + ]))); + + Route::middleware([StartSession::class, WithAllErrorsMiddleware::class])->get('/', function () { + $errors = Inertia::getShared('errors')(); + + $this->assertIsObject($errors); + $this->assertSame(['The name field is required.'], $errors->name); + $this->assertSame( + ['Not a valid email address.', 'Another email error.'], + $errors->email + ); + }); + + $this->withoutExceptionHandling()->get('/'); + } + public function test_validation_errors_with_named_error_bags_are_scoped(): void { Session::put('errors', (new ViewErrorBag)->put('example', new MessageBag([ diff --git a/tests/Stubs/WithAllErrorsMiddleware.php b/tests/Stubs/WithAllErrorsMiddleware.php new file mode 100644 index 00000000..c4a68b78 --- /dev/null +++ b/tests/Stubs/WithAllErrorsMiddleware.php @@ -0,0 +1,11 @@ +