Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Session;
Expand Down Expand Up @@ -60,9 +61,17 @@ protected function shareValidationErrors()
}

Inertia::share('errors', function () {
return (object) array_map(function ($error) {
return $error[0];
}, Session::has('errors') ? Session::get('errors')->messages() : []);
if (! Session::has('errors')) {
return (object) [];
}

return (object) Collection::make(Session::get('errors')->getBags())->map(function ($bag) {
return (object) Collection::make($bag->messages())->map(function ($errors) {
return $errors[0];
})->toArray();
})->pipe(function ($bags) {
return $bags->has('default') ? $bags->get('default') : $bags->toArray();
});
});
}
}
29 changes: 26 additions & 3 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ViewErrorBag;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -56,7 +57,15 @@ public function test_middleware_is_registered()

public function test_validation_errors_are_registered()
{
$this->assertTrue(Inertia::getShared('errors') instanceof Closure);
$this->assertInstanceOf(Closure::class, Inertia::getShared('errors'));
}

public function test_validation_errors_can_be_empty()
{
$errors = Inertia::getShared('errors')();

$this->assertIsObject($errors);
$this->assertEmpty(get_object_vars($errors));
}

public function test_validation_errors_are_not_registered_when_already_registered()
Expand All @@ -68,15 +77,29 @@ public function test_validation_errors_are_not_registered_when_already_registere

public function test_validation_errors_are_returned_in_the_correct_format()
{
Session::put('errors', new MessageBag([
Session::put('errors', (new ViewErrorBag())->put('default', new MessageBag([
'name' => 'The name field is required.',
'email' => 'Not a valid email address.',
]));
])));

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

$this->assertIsObject($errors);
$this->assertSame('The name field is required.', $errors->name);
$this->assertSame('Not a valid email address.', $errors->email);
}

public function test_validation_errors_with_named_error_bags_are_scoped()
{
Session::put('errors', (new ViewErrorBag())->put('example', new MessageBag([
'name' => 'The name field is required.',
'email' => 'Not a valid email address.',
])));

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

$this->assertIsObject($errors);
$this->assertSame('The name field is required.', $errors->example->name);
$this->assertSame('Not a valid email address.', $errors->example->email);
}
}