Skip to content

Commit 38be8f9

Browse files
committed
Add error bag support
1 parent 19d96c2 commit 38be8f9

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

src/ServiceProvider.php

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

55
use Illuminate\Http\Request;
66
use Illuminate\Routing\Router;
7+
use Illuminate\Support\MessageBag;
8+
use Illuminate\Support\ViewErrorBag;
79
use Illuminate\Contracts\Http\Kernel;
810
use Illuminate\Support\Facades\Blade;
911
use Illuminate\Support\Facades\Session;
@@ -60,9 +62,27 @@ protected function shareValidationErrors()
6062
}
6163

6264
Inertia::share('errors', function () {
63-
return (object) array_map(function ($error) {
64-
return $error[0];
65-
}, Session::has('errors') ? Session::get('errors')->messages() : []);
65+
$errors = Session::get('errors', new ViewErrorBag());
66+
67+
if (is_array($errors) && count($errors) > 0) {
68+
$errors = (new ViewErrorBag())->put('default', new MessageBag($errors));
69+
} elseif ($errors instanceof MessageBag && $errors->any()) {
70+
$errors = (new ViewErrorBag())->put('default', $errors);
71+
} elseif (! $errors instanceof ViewErrorBag) {
72+
return (object) [];
73+
}
74+
75+
$formatted = collect($errors->getBags())->map(function (MessageBag $bag) {
76+
return collect($bag->messages())->map(function ($errors) {
77+
return $errors[0];
78+
});
79+
});
80+
81+
if ($formatted->count() === 1 && $formatted->has('default')) {
82+
return (object) $formatted->toArray()['default'];
83+
}
84+
85+
return (object) $formatted->toArray();
6686
});
6787
}
6888
}

tests/ServiceProviderTest.php

Lines changed: 64 additions & 4 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()
@@ -66,17 +75,68 @@ public function test_validation_errors_are_not_registered_when_already_registere
6675
$this->assertSame('This is a validation error', Inertia::getShared('errors'));
6776
}
6877

69-
public function test_validation_errors_are_returned_in_the_correct_format()
78+
public function test_validation_errors_can_be_an_array()
79+
{
80+
Session::put('errors', [
81+
'name' => 'The name field is required.',
82+
'email' => 'The email must be a valid email address.',
83+
]);
84+
85+
$errors = Inertia::getShared('errors')();
86+
87+
$this->assertIsObject($errors);
88+
$this->assertSame('The name field is required.', $errors->name);
89+
$this->assertSame('The email must be a valid email address.', $errors->email);
90+
}
91+
92+
public function test_validation_exceptions_can_be_a_message_bag()
7093
{
7194
Session::put('errors', new MessageBag([
7295
'name' => 'The name field is required.',
73-
'email' => 'Not a valid email address',
96+
'email' => 'The email must be a valid email address.',
7497
]));
7598

7699
$errors = Inertia::getShared('errors')();
77100

78101
$this->assertIsObject($errors);
79102
$this->assertSame('The name field is required.', $errors->name);
80-
$this->assertSame('Not a valid email address', $errors->email);
103+
$this->assertSame('The email must be a valid email address.', $errors->email);
104+
}
105+
106+
public function test_validation_exceptions_can_be_an_error_bag()
107+
{
108+
Session::put('errors', (new ViewErrorBag())->put('default', new MessageBag([
109+
'name' => 'The name field is required.',
110+
'email' => 'The email must be a valid email address.',
111+
])));
112+
113+
$errors = Inertia::getShared('errors')();
114+
115+
$this->assertIsObject($errors);
116+
$this->assertSame('The name field is required.', $errors->name);
117+
$this->assertSame('The email must be a valid email address.', $errors->email);
118+
}
119+
120+
public function test_validation_exceptions_can_be_multiple_error_bags()
121+
{
122+
Session::put('errors', tap(new ViewErrorBag(), function ($errorBags) {
123+
$errorBags->put('default', new MessageBag(['name' => 'The name field is required.']));
124+
$errorBags->put('example', new MessageBag(['email' => 'The email must be a valid email address.']));
125+
}));
126+
127+
$errors = Inertia::getShared('errors')();
128+
129+
$this->assertIsObject($errors);
130+
$this->assertSame('The name field is required.', $errors->default['name']);
131+
$this->assertSame('The email must be a valid email address.', $errors->example['email']);
132+
}
133+
134+
public function test_validation_exceptions_will_be_empty_when_an_invalid_value_was_set_to_the_session()
135+
{
136+
Session::put('errors', new Request());
137+
$errors = Inertia::getShared('errors')();
138+
139+
$this->assertIsObject($errors);
140+
$this->assertEmpty(get_object_vars($errors));
81141
}
82142
}

0 commit comments

Comments
 (0)