Support the ongoing development of Laravel.io →
posted 3 years ago
Validation
Last updated 1 year ago.
0
moderator

In app/Exceptions/Handler.php you add the convertValidationExceptionToResponse function to overwrite the default (see the original from https://github.com/laravel/framework/blob/8.x/src/Illuminate/F... )
That gives you the option to customize the output to the format that you want. I hope this is a good starting point for you :)

a-h-abid liked this reply

1

Hey thank @Tobias, that is a good start point, but that way I wont be able to get to know for which rule the message is for.

Anyway I kind of digged down the source code and this is what I came up with.

I created a middleware on which I set my own validator as the validator resolver. The validator also extends the laravel's validator. With your help from example above, I modified my validator to get my desired response.

Currently it works well for API responses. But may not work well in blade view files due to MessageBag class. Maybe someone finds a better implementation someday. Or maybe even added in Core.

iboldurev, tvbeek liked this reply

2

Sign in to participate in this thread!

Eventy

Your banner here too?

a-h-abid a-h-abid Joined 17 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.