Skip to content

Commit 507fb83

Browse files
authored
Simplify named error bags implementation
1 parent dd793af commit 507fb83

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/ServiceProvider.php

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

55
use Illuminate\Http\Request;
66
use Illuminate\Routing\Router;
7-
use Illuminate\Support\MessageBag;
8-
use Illuminate\Support\ViewErrorBag;
97
use Illuminate\Contracts\Http\Kernel;
108
use Illuminate\Support\Facades\Blade;
119
use Illuminate\Support\Facades\Session;
@@ -62,27 +60,17 @@ protected function shareValidationErrors()
6260
}
6361

6462
Inertia::share('errors', function () {
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) {
63+
if (! Session::has('errors')) {
7264
return (object) [];
7365
}
7466

75-
$formatted = collect($errors->getBags())->map(function (MessageBag $bag) {
67+
return (object) collect(Session::get('errors')->getBags())->map(function ($bag) {
7668
return collect($bag->messages())->map(function ($errors) {
7769
return $errors[0];
7870
});
79-
});
80-
81-
if ($formatted->count() === 1 && $formatted->has('default')) {
82-
return (object) $formatted->toArray()['default'];
83-
}
84-
85-
return (object) $formatted->toArray();
71+
})->pipe(function ($bags) {
72+
return $bags->has('default') ? $bags->get('default') : $bags;
73+
})->toArray();
8674
});
8775
}
8876
}

0 commit comments

Comments
 (0)