Skip to content

Commit 458366a

Browse files
authored
Merge pull request #137 from claudiodekker/validation-errors
Add support for (multiple) error bags
2 parents 36b1049 + bc72ebd commit 458366a

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/ServiceProvider.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Http\Request;
66
use Illuminate\Routing\Router;
7+
use Illuminate\Support\Collection;
78
use Illuminate\Contracts\Http\Kernel;
89
use Illuminate\Support\Facades\Blade;
910
use Illuminate\Support\Facades\Session;
@@ -60,9 +61,17 @@ protected function shareValidationErrors()
6061
}
6162

6263
Inertia::share('errors', function () {
63-
return (object) array_map(function ($error) {
64-
return $error[0];
65-
}, Session::has('errors') ? Session::get('errors')->messages() : []);
64+
if (! Session::has('errors')) {
65+
return (object) [];
66+
}
67+
68+
return (object) Collection::make(Session::get('errors')->getBags())->map(function ($bag) {
69+
return (object) Collection::make($bag->messages())->map(function ($errors) {
70+
return $errors[0];
71+
})->toArray();
72+
})->pipe(function ($bags) {
73+
return $bags->has('default') ? $bags->get('default') : $bags->toArray();
74+
});
6675
});
6776
}
6877
}

tests/ServiceProviderTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Http\Request;
99
use Illuminate\Support\MessageBag;
1010
use Illuminate\Support\Facades\App;
11+
use Illuminate\Support\ViewErrorBag;
1112
use Illuminate\Contracts\Http\Kernel;
1213
use Illuminate\Support\Facades\Blade;
1314
use Illuminate\Support\Facades\Route;
@@ -56,7 +57,15 @@ public function test_middleware_is_registered()
5657

5758
public function test_validation_errors_are_registered()
5859
{
59-
$this->assertTrue(Inertia::getShared('errors') instanceof Closure);
60+
$this->assertInstanceOf(Closure::class, Inertia::getShared('errors'));
61+
}
62+
63+
public function test_validation_errors_can_be_empty()
64+
{
65+
$errors = Inertia::getShared('errors')();
66+
67+
$this->assertIsObject($errors);
68+
$this->assertEmpty(get_object_vars($errors));
6069
}
6170

6271
public function test_validation_errors_are_not_registered_when_already_registered()
@@ -68,15 +77,29 @@ public function test_validation_errors_are_not_registered_when_already_registere
6877

6978
public function test_validation_errors_are_returned_in_the_correct_format()
7079
{
71-
Session::put('errors', new MessageBag([
80+
Session::put('errors', (new ViewErrorBag())->put('default', new MessageBag([
7281
'name' => 'The name field is required.',
7382
'email' => 'Not a valid email address.',
74-
]));
83+
])));
7584

7685
$errors = Inertia::getShared('errors')();
7786

7887
$this->assertIsObject($errors);
7988
$this->assertSame('The name field is required.', $errors->name);
8089
$this->assertSame('Not a valid email address.', $errors->email);
8190
}
91+
92+
public function test_validation_errors_with_named_error_bags_are_scoped()
93+
{
94+
Session::put('errors', (new ViewErrorBag())->put('example', new MessageBag([
95+
'name' => 'The name field is required.',
96+
'email' => 'Not a valid email address.',
97+
])));
98+
99+
$errors = Inertia::getShared('errors')();
100+
101+
$this->assertIsObject($errors);
102+
$this->assertSame('The name field is required.', $errors->example->name);
103+
$this->assertSame('Not a valid email address.', $errors->example->email);
104+
}
82105
}

0 commit comments

Comments
 (0)