how can I translate "The given data was invalid." to Laravel 5.6? Thank you
how can I translate "The given data was invalid." to Laravel 5.6? Thank you
find and replace that message in resources/lang/{lang_code}/validation
'exists' => 'The selected :attribute is invalid.',
change here with your language instead of :attribute
OR
add below lines added into render() method of the file app\Exceptions\Handler.php
if ($exception instanceof ValidationException)
return response()->json(['message' => 'Your error message here', 'errors' => $exception->validator->getMessageBag()], 422); //type your error code.
Happy coding~! :)
"The given data was invalid." is hard coded
File: src/Illuminate/Validation/ValidationException.php
public function __construct($validator, $response = null, $errorBag = 'default')
{
- parent::__construct('The given data was invalid.');
+ parent::__construct(__('The given data was invalid.'));
$this->response = $response;
$this->errorBag = $errorBag;
From commit: https://github.com/laravel/framework/pull/22112/commits/b70372fd0031e5fabaee462c913b19b665becaf3
As JsWizard already mentioned above you can handle that specific exception by adding following lines to app\Exceptions\Handler.php
Just to improve his answer, you need to include ValidationException
use Illuminate\Validation\ValidationException as ValidationException;
and then add following to the render() method:
if ($exception instanceof ValidationException) {
return response()->json(['message' => 'YOUR CUSTOM MESSAGE HERE', 'errors' => $exception->validator->getMessageBag()], 422);
}
in laravel 8 app/Exceptions/Handler.php just overwrite the method as following
/**
* Convert a validation exception into a JSON response.
*
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Validation\ValidationException $exception
* @return \Illuminate\Http\JsonResponse
*/
protected function invalidJson($request, ValidationException $exception)
{
return response()->json([
'message' => __('validation.headermsg'),
'errors' => $exception->errors(),
], $exception->status);
}
thats it enjoy
Sorry for late reply, I found a method working in Laravel 5.8 and above. I hope you were still looking for an answer, here is mine.
As Official Docs from Laravel explains:
When using the
validatemethod during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.
So, you cannot translate the response, because is part of the core of laravel and is hardcoded, and do the same in the response is not a solution.
So I recommend you to use the Request Validation method for your forms, and then extend the method called failedValidation(Validator $validator) which is part of FormRequest class.
You can create a new RequestValidation with the artisan console:
php artisan make:request FooRequest
When you had your FooRequest.php file, add the method failedValidation method with the suggested lines:
/**
* Handle a failed validation attempt.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function failedValidation(Validator $validator)
{
/**
* @var array $response Is our response data.
*/
$response = [
"success" => false, // Here I added a new field on JSON response.
"message" => __("Los datos enviados no son vĂ¡lidos."), // Here I used a custom message.
"errors" => $validator->errors(), // And do not forget to add the common errors.
];
// Finally throw the HttpResponseException.
throw new HttpResponseException(response()->json($response, 422));
}
And the result of our 422 responses is:
I used Postman for the Request, check the response.
I hope it helps for future references.
Best Regards
Sadly the Laravel team marked this as a non issue (they're pretty stubborn in this kind of things).
The cleanest way i found to circumnvent the issue is to add to app/Exceptions/Handler.php this code:
public function render($request, Throwable $e)
{
if ($e instanceof ValidationException)
{
if ($request->expectsJson()) {
return response()->json([
'message'=>__($e->getMessage()),
'errors'=> $e->errors(),
],$e->status);
}
}
return parent::render($request, $e);
}
Then add the translation in resources/lang/[language-code].json; for example for italy create a file named resources/lang/it.json with this content:
{
"The given data was invalid." : "I dati inseriti contengono errori."
}
The code will only replace the message string in JSON response (as needed).