Skip to content

Commit 79b6ee4

Browse files
committed
Support for multiple errors per field
1 parent 98c5ec3 commit 79b6ee4

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/Middleware.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class Middleware
2121
*/
2222
protected $rootView = 'app';
2323

24+
/**
25+
* Determines if validation errors should be mapped to a single error message per field.
26+
*
27+
* @var bool
28+
*/
29+
protected $singleErrorPerField = true;
30+
2431
/**
2532
* Determine the current asset version.
2633
*
@@ -153,7 +160,7 @@ public function resolveValidationErrors(Request $request)
153160

154161
return (object) collect($bags)->map(function ($bag) {
155162
return (object) collect($bag->messages())->map(function ($errors) {
156-
return $errors[0];
163+
return $this->singleErrorPerField ? $errors[0] : $errors;
157164
})->toArray();
158165
})->pipe(function ($bags) use ($request) {
159166
if ($bags->has('default') && $request->header(Header::ERROR_BAG)) {

tests/MiddlewareTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Inertia\AlwaysProp;
1414
use Inertia\Inertia;
1515
use Inertia\Middleware;
16+
use Inertia\Tests\Stubs\ArrayErrorsMiddleware;
1617
use Inertia\Tests\Stubs\CustomUrlResolverMiddleware;
1718
use Inertia\Tests\Stubs\ExampleMiddleware;
1819
use LogicException;
@@ -168,11 +169,11 @@ public function test_validation_errors_can_be_empty(): void
168169
$this->withoutExceptionHandling()->get('/');
169170
}
170171

171-
public function test_validation_errors_are_returned_in_the_correct_format(): void
172+
public function test_validation_errors_are_mapped_to_strings_by_default(): void
172173
{
173174
Session::put('errors', (new ViewErrorBag)->put('default', new MessageBag([
174-
'name' => 'The name field is required.',
175-
'email' => 'Not a valid email address.',
175+
'name' => ['The name field is required.'],
176+
'email' => ['Not a valid email address.', 'Another email error.'],
176177
])));
177178

178179
Route::middleware([StartSession::class, ExampleMiddleware::class])->get('/', function () {
@@ -186,6 +187,27 @@ public function test_validation_errors_are_returned_in_the_correct_format(): voi
186187
$this->withoutExceptionHandling()->get('/');
187188
}
188189

190+
public function test_validation_errors_can_remain_multiple_per_field(): void
191+
{
192+
Session::put('errors', (new ViewErrorBag)->put('default', new MessageBag([
193+
'name' => ['The name field is required.'],
194+
'email' => ['Not a valid email address.', 'Another email error.'],
195+
])));
196+
197+
Route::middleware([StartSession::class, ArrayErrorsMiddleware::class])->get('/', function () {
198+
$errors = Inertia::getShared('errors')();
199+
200+
$this->assertIsObject($errors);
201+
$this->assertSame(['The name field is required.'], $errors->name);
202+
$this->assertSame(
203+
['Not a valid email address.', 'Another email error.'],
204+
$errors->email
205+
);
206+
});
207+
208+
$this->withoutExceptionHandling()->get('/');
209+
}
210+
189211
public function test_validation_errors_with_named_error_bags_are_scoped(): void
190212
{
191213
Session::put('errors', (new ViewErrorBag)->put('example', new MessageBag([
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Inertia\Tests\Stubs;
4+
5+
class ArrayErrorsMiddleware extends ExampleMiddleware
6+
{
7+
/**
8+
* @var bool
9+
*/
10+
protected $singleErrorPerField = false;
11+
}

0 commit comments

Comments
 (0)