diff --git a/src/Middleware.php b/src/Middleware.php index 81b6ab9d..4fedcc09 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 $singleErrorPerField = true; + /** * 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->singleErrorPerField ? $errors[0] : $errors; })->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..90d96ce1 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -13,6 +13,7 @@ use Inertia\AlwaysProp; use Inertia\Inertia; use Inertia\Middleware; +use Inertia\Tests\Stubs\ArrayErrorsMiddleware; use Inertia\Tests\Stubs\CustomUrlResolverMiddleware; use Inertia\Tests\Stubs\ExampleMiddleware; use LogicException; @@ -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, ArrayErrorsMiddleware::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/ArrayErrorsMiddleware.php b/tests/Stubs/ArrayErrorsMiddleware.php new file mode 100644 index 00000000..5ad843cc --- /dev/null +++ b/tests/Stubs/ArrayErrorsMiddleware.php @@ -0,0 +1,11 @@ +