4

I have some validations in place, and I am validating it like this:

validator make

and I noticed when there is more than 1 error, laravel will add to the message "and 1(or 2 or 4..) more error(s)" ..

Take a look at the images and you will see what I mean:

validator message response

I found out where this is happening inside laravel framework / which function:

Validation Exception summarize function

it calls the Translater get method with a dynamic key! How would I translate dynamic keys??

translator interface

I tried a few things:

  • Adding the key with the place holder in both English/Portuguese "validation" translation file
  • Add the key with the number itself (replaced placeholder)

none took effect, see what I mean below:

dynamic translation key with placeholder translation key with replaced placeholder

I've noticed there are other people with the same issue when searching in google but so far I have not found a solution, I would like not to have to do a "string replace" and use a more elegant solution... Any one could help?

other people with the same issue: https://github.com/laravel/framework/discussions/41230

Thanks alot

Pablo Camara
  • 460
  • 4
  • 13

3 Answers3

5

The (and :count more error) and (and :count more errors) need to be added to your json translation files as this is not a "short key" but a "translation string": https://laravel.com/docs/9.x/localization#using-translation-strings-as-keys

Dennis
  • 200
  • 15
  • 2
    Do not post ***Link Only Answer***. It's a lousy way of answering. Add relevant code or point out some essential matters/points in your answer. – Abdulla Nilam Sep 30 '22 at 08:05
  • @AbdullaNilam The answer is in the sentence before the link it just has to be read. The `(and :count more error)` must simply be added to the JSON translation file, thats it. I think a reference to the docs is better that copying the contents of the docs in here. After a while this can be outdated and it won't help anyone but confuse them. – Dennis Sep 30 '22 at 12:05
  • I'd like to point out that sometimes the json file might not exists. You need to create one under the folder YourProject / Lang / fr.json. – Linesofcode Aug 14 '23 at 17:17
2

Solution proposed by Dennis works, but if you are not interested in displaying the exact number of errors and you would like to replace the whole message with something more generic you can also consider to override the invalidJson() method in app/Exceptions/Handler.php:

protected function invalidJson($request, ValidationException $exception)
{
    return response()->json([
        'message' => __('Some of the fields are not valid'),
        'errors' => $exception->errors(),
    ], $exception->status);
}

This will work if you are working with API routes.

xonya
  • 2,146
  • 29
  • 37
  • Do not forget to add use case to the top of the page: use Illuminate\Validation\ValidationException; This method worked for me. Thanks. – Sinan Eldem Mar 02 '23 at 19:19
0

A quick fix / solution that works/worked for my case ( because I only need to display first error, one at a time, and not multiple )

this will remove the extra (and 1 more error.. ):

remove and 1 more error from laravel validation exception summarize

In some cases you will also need to add the "bail" in the validation rules (to avoid more than 1 error per field)

Pablo Camara
  • 460
  • 4
  • 13