Skip to content

Conversation

@reinink
Copy link
Member

@reinink reinink commented Sep 1, 2020

This updates this adapter to automatically share validation errors with Inertia.js, instead of making Laravel users do this manually, like this:

Inertia::share([
    'errors' => function () {
        return Session::get('errors')
            ? Session::get('errors')->getBag('default')->getMessages()
            : (object) [];
    },
]);

The key difference with the above method is that Inertia will automatically return only one error per field, instead of the Laravel default, which is an array of errors.

{
  name: 'The name field is required.',
  email: 'Not a valid email address.',
}

This is simpler, and honestly, I've literally never displayed multiple errors per field. This seems to be the consensus on Twitter as well.

That said, if folks prefer the other behaviour (multiple errors per field), all they need to do is manually share the validation errors like before (using the snippet above). This package's service provider will only share the validation errors if the errors key has not already been used. This behaviour also makes this a non-breaking change.

The goal of this change is to make Inertia.js easier to setup in Laravel applications, and since sharing validation errors is so common, this seems worthwhile to bake into the adapter.

@herpaderpaldent
Copy link

thank you @reinink for this PR, i really like to see this change merged. May i point out that the documentation should be updated accordingly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants