Skip to content

Commit fb8616a

Browse files
committed
Support array-based error bags + use data provider
1 parent 38be8f9 commit fb8616a

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

src/ServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ protected function shareValidationErrors()
6464
Inertia::share('errors', function () {
6565
$errors = Session::get('errors', new ViewErrorBag());
6666

67-
if (is_array($errors) && count($errors) > 0) {
67+
if (is_array($errors) && count($errors) > 0 && is_array($errors[array_keys($errors)[0]])) {
68+
$errors = tap(new ViewErrorBag(), function ($bag) use ($errors) {
69+
foreach ($errors as $key => $messages) {
70+
$bag->put($key, new MessageBag($messages));
71+
}
72+
});
73+
} elseif (is_array($errors) && count($errors) > 0) {
6874
$errors = (new ViewErrorBag())->put('default', new MessageBag($errors));
6975
} elseif ($errors instanceof MessageBag && $errors->any()) {
7076
$errors = (new ViewErrorBag())->put('default', $errors);

tests/ServiceProviderTest.php

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,40 @@ public function test_validation_errors_are_not_registered_when_already_registere
7575
$this->assertSame('This is a validation error', Inertia::getShared('errors'));
7676
}
7777

78-
public function test_validation_errors_can_be_an_array()
78+
public function basicValidationErrorsProvider()
7979
{
80-
Session::put('errors', [
80+
$array = [
81+
'name' => 'The name field is required.',
82+
'email' => [
83+
'The email must be a valid email address.',
84+
],
85+
];
86+
87+
$messageBag = new MessageBag([
8188
'name' => 'The name field is required.',
8289
'email' => 'The email must be a valid email address.',
8390
]);
8491

85-
$errors = Inertia::getShared('errors')();
92+
$viewErrorBag = (new ViewErrorBag())->put('default', new MessageBag([
93+
'name' => [
94+
'The name field is required.',
95+
],
96+
'email' => 'The email must be a valid email address.',
97+
]));
8698

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);
99+
return [
100+
[$array],
101+
[$messageBag],
102+
[$viewErrorBag],
103+
];
90104
}
91105

92-
public function test_validation_exceptions_can_be_a_message_bag()
106+
/**
107+
* @dataProvider basicValidationErrorsProvider
108+
*/
109+
public function test_validation_errors_are_returned_in_the_correct_format($errors)
93110
{
94-
Session::put('errors', new MessageBag([
95-
'name' => 'The name field is required.',
96-
'email' => 'The email must be a valid email address.',
97-
]));
111+
Session::put('errors', $errors);
98112

99113
$errors = Inertia::getShared('errors')();
100114

@@ -103,26 +117,44 @@ public function test_validation_exceptions_can_be_a_message_bag()
103117
$this->assertSame('The email must be a valid email address.', $errors->email);
104118
}
105119

106-
public function test_validation_exceptions_can_be_an_error_bag()
120+
public function multipleErrorBagsValidationErrorsProvider()
107121
{
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);
122+
$array = [
123+
'default' => [
124+
'name' => [
125+
'The name field is required.',
126+
],
127+
'email' => 'The email must be a valid email address.',
128+
],
129+
'example' => [
130+
'name' => 'The name field is required.',
131+
'email' => 'The email must be a valid email address.',
132+
],
133+
];
134+
135+
$viewErrorBag = tap(new ViewErrorBag(), function ($errorBag) {
136+
$errorBag->put('default', new MessageBag([
137+
'name' => 'The name field is required.',
138+
'email' => 'The email must be a valid email address.',
139+
]));
140+
$errorBag->put('example', new MessageBag([
141+
'name' => 'The name field is required.',
142+
'email' => [
143+
'The email must be a valid email address.',
144+
],
145+
]));
146+
});
147+
148+
return [
149+
[$array],
150+
[$viewErrorBag],
151+
];
118152
}
119153

120-
public function test_validation_exceptions_can_be_multiple_error_bags()
154+
/** @dataProvider multipleErrorBagsValidationErrorsProvider */
155+
public function test_validation_exceptions_can_have_multiple_error_bags($errors)
121156
{
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-
}));
157+
Session::put('errors', $errors);
126158

127159
$errors = Inertia::getShared('errors')();
128160

0 commit comments

Comments
 (0)