翻译“给定的数据无效。” 在 Laravel 5.6 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53882542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Translate "The given data was invalid." in Laravel 5.6
提问by enzo
how can I translate "The given data was invalid." to Laravel 5.6? Thank you
我该如何翻译“给定的数据无效”。到 Laravel 5.6?谢谢
回答by JsWizard
find and replace that message in resources/lang/{lang_code}/validation
找到并替换该消息 resources/lang/{lang_code}/validation
'exists' => 'The selected :attribute is invalid.',
change here with your language instead of :attribute
在这里用你的语言而不是 :attribute
OR
或者
add below lines added into render() method of the file app\Exceptions\Handler.php
添加以下行添加到文件的 render() 方法中 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~! :)
编码愉快~!:)
回答by Decu
"The given data was invalid." is hard coded
“给定的数据无效。” 是硬编码的
File: src/Illuminate/Validation/ValidationException.php
文件: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
从提交:https: //github.com/laravel/framework/pull/22112/commits/b70372fd0031e5fabaee462c913b19b665becaf3
回答by vstyler96
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.
抱歉回复晚了,我找到了一种适用于 Laravel 5.8 及更高版本的方法。我希望你还在寻找答案,这是我的。
As Official Docs from Laravel explains:
正如 Laravel 的官方文档所解释的:
When using the
validate
method 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.
validate
在 AJAX 请求期间使用该方法时,Laravel 不会生成重定向响应。相反,Laravel 会生成一个包含所有验证错误的 JSON 响应。此 JSON 响应将与 422 HTTP 状态代码一起发送。
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.
所以,你不能翻译响应,因为它是 laravel 核心的一部分并且是硬编码的,在响应中做同样的事情不是解决方案。
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.
所以我建议你对你的表单使用请求验证方法,然后扩展调用的方法failedValidation(Validator $validator)
,它是 FormRequest 类的一部分。
You can create a new RequestValidation with the artisan console:
php artisan make:request FooRequest
您可以使用 artisan 控制台创建一个新的 RequestValidation:
php artisan make:request FooRequest
When you had your FooRequest.php
file, add the method failedValidationmethod with the suggested lines:
获得FooRequest.php
文件后,使用建议的行添加方法failedValidation方法:
/**
* 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:
我们的 422 个回复的结果是:
I used Postman for the Request, check the response.
I hope it helps for future references.
我使用邮递员进行请求,检查响应。
我希望它有助于以后的参考。
Best Regards
此致
回答by Luka Sh
As JsWizard already mentioned above you can handle that specific exception by adding following lines to app\Exceptions\Handler.php
正如上面已经提到的 JsWizard,您可以通过添加以下行来处理该特定异常 app\Exceptions\Handler.php
Just to improve his answer, you need to include ValidationException
只是为了改进他的答案,您需要包含 ValidationException
use Illuminate\Validation\ValidationException as ValidationException;
and then add following to the render()
method:
然后在render()
方法中添加以下内容:
if ($exception instanceof ValidationException) {
return response()->json(['message' => 'YOUR CUSTOM MESSAGE HERE', 'errors' => $exception->validator->getMessageBag()], 422);
}