how can I translate "The given data was invalid." to Laravel 5.6? Thank you
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With