Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate "The given data was invalid." in Laravel 5.6

how can I translate "The given data was invalid." to Laravel 5.6? Thank you

like image 881
enzo Avatar asked Dec 21 '18 09:12

enzo


3 Answers

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

like image 113
Mike Mulaffer Avatar answered Sep 20 '22 16:09

Mike Mulaffer


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~! :)

like image 15
JsWizard Avatar answered Oct 13 '22 17:10

JsWizard


"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

like image 11
Decu Avatar answered Oct 13 '22 18:10

Decu