Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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)) {
Expand Down
28 changes: 25 additions & 3 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand All @@ -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([
Expand Down
11 changes: 11 additions & 0 deletions tests/Stubs/ArrayErrorsMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Inertia\Tests\Stubs;

class ArrayErrorsMiddleware extends ExampleMiddleware
{
/**
* @var bool
*/
protected $singleErrorPerField = false;
}