Skip to content

Commit 3e2a01f

Browse files
authored
Merge pull request #135 from inertiajs/validation-errors
Automatically share validation errors
2 parents 1648fd1 + 458366a commit 3e2a01f

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

src/ServiceProvider.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
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;
10+
use Illuminate\Support\Facades\Session;
911
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
1012

1113
class ServiceProvider extends BaseServiceProvider
@@ -21,6 +23,7 @@ public function boot()
2123
$this->registerRequestMacro();
2224
$this->registerRouterMacro();
2325
$this->registerMiddleware();
26+
$this->shareValidationErrors();
2427
}
2528

2629
protected function registerBladeDirective()
@@ -50,4 +53,25 @@ protected function registerMiddleware()
5053
{
5154
$this->app[Kernel::class]->pushMiddleware(Middleware::class);
5255
}
56+
57+
protected function shareValidationErrors()
58+
{
59+
if (Inertia::getShared('errors')) {
60+
return;
61+
}
62+
63+
Inertia::share('errors', function () {
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+
});
75+
});
76+
}
5377
}

tests/ControllerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public function test_controller_returns_an_inertia_response()
2828
$this->assertEquals([
2929
'page' => [
3030
'component' => 'User/Edit',
31-
'props' => ['user' => ['name' => 'Jonathan']],
31+
'props' => [
32+
'user' => ['name' => 'Jonathan'],
33+
'errors' => (object) [],
34+
],
3235
'url' => '',
3336
'version' => null,
3437
],

tests/ServiceProviderTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Inertia\Tests;
44

5+
use Closure;
6+
use Inertia\Inertia;
57
use Inertia\Middleware;
68
use Illuminate\Http\Request;
9+
use Illuminate\Support\MessageBag;
710
use Illuminate\Support\Facades\App;
11+
use Illuminate\Support\ViewErrorBag;
812
use Illuminate\Contracts\Http\Kernel;
913
use Illuminate\Support\Facades\Blade;
1014
use Illuminate\Support\Facades\Route;
15+
use Illuminate\Support\Facades\Session;
1116

1217
class ServiceProviderTest extends TestCase
1318
{
@@ -49,4 +54,52 @@ public function test_middleware_is_registered()
4954

5055
$this->assertTrue($kernel->hasMiddleware(Middleware::class));
5156
}
57+
58+
public function test_validation_errors_are_registered()
59+
{
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));
69+
}
70+
71+
public function test_validation_errors_are_not_registered_when_already_registered()
72+
{
73+
Inertia::share('errors', 'This is a validation error');
74+
75+
$this->assertSame('This is a validation error', Inertia::getShared('errors'));
76+
}
77+
78+
public function test_validation_errors_are_returned_in_the_correct_format()
79+
{
80+
Session::put('errors', (new ViewErrorBag())->put('default', new MessageBag([
81+
'name' => 'The name field is required.',
82+
'email' => 'Not 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('Not a valid email address.', $errors->email);
90+
}
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+
}
52105
}

0 commit comments

Comments
 (0)