Skip to content

Commit 19d96c2

Browse files
committed
Automatically share validation errors
1 parent 1648fd1 commit 19d96c2

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

src/ServiceProvider.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Routing\Router;
77
use Illuminate\Contracts\Http\Kernel;
88
use Illuminate\Support\Facades\Blade;
9+
use Illuminate\Support\Facades\Session;
910
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
1011

1112
class ServiceProvider extends BaseServiceProvider
@@ -21,6 +22,7 @@ public function boot()
2122
$this->registerRequestMacro();
2223
$this->registerRouterMacro();
2324
$this->registerMiddleware();
25+
$this->shareValidationErrors();
2426
}
2527

2628
protected function registerBladeDirective()
@@ -50,4 +52,17 @@ protected function registerMiddleware()
5052
{
5153
$this->app[Kernel::class]->pushMiddleware(Middleware::class);
5254
}
55+
56+
protected function shareValidationErrors()
57+
{
58+
if (Inertia::getShared('errors')) {
59+
return;
60+
}
61+
62+
Inertia::share('errors', function () {
63+
return (object) array_map(function ($error) {
64+
return $error[0];
65+
}, Session::has('errors') ? Session::get('errors')->messages() : []);
66+
});
67+
}
5368
}

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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
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;
811
use Illuminate\Contracts\Http\Kernel;
912
use Illuminate\Support\Facades\Blade;
1013
use Illuminate\Support\Facades\Route;
14+
use Illuminate\Support\Facades\Session;
1115

1216
class ServiceProviderTest extends TestCase
1317
{
@@ -49,4 +53,30 @@ public function test_middleware_is_registered()
4953

5054
$this->assertTrue($kernel->hasMiddleware(Middleware::class));
5155
}
56+
57+
public function test_validation_errors_are_registered()
58+
{
59+
$this->assertTrue(Inertia::getShared('errors') instanceof Closure);
60+
}
61+
62+
public function test_validation_errors_are_not_registered_when_already_registered()
63+
{
64+
Inertia::share('errors', 'This is a validation error');
65+
66+
$this->assertSame('This is a validation error', Inertia::getShared('errors'));
67+
}
68+
69+
public function test_validation_errors_are_returned_in_the_correct_format()
70+
{
71+
Session::put('errors', new MessageBag([
72+
'name' => 'The name field is required.',
73+
'email' => 'Not a valid email address',
74+
]));
75+
76+
$errors = Inertia::getShared('errors')();
77+
78+
$this->assertIsObject($errors);
79+
$this->assertSame('The name field is required.', $errors->name);
80+
$this->assertSame('Not a valid email address', $errors->email);
81+
}
5282
}

0 commit comments

Comments
 (0)