JsW*_*ard 11
找到并替换该消息 resources/lang/{lang_code}/validation
'exists' => 'The selected :attribute is invalid.',
Run Code Online (Sandbox Code Playgroud)
在这里用你的语言而不是 :attribute
或者
添加以下行添加到文件的 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.
Run Code Online (Sandbox Code Playgroud)
编码愉快~!:)
正如上面已经提到的 JsWizard,您可以通过添加以下行来处理该特定异常 app\Exceptions\Handler.php
只是为了改进他的答案,您需要包含 ValidationException
use Illuminate\Validation\ValidationException as ValidationException;
Run Code Online (Sandbox Code Playgroud)
然后在render()方法中添加以下内容:
if ($exception instanceof ValidationException) {
return response()->json(['message' => 'YOUR CUSTOM MESSAGE HERE', 'errors' => $exception->validator->getMessageBag()], 422);
}
Run Code Online (Sandbox Code Playgroud)
小智 7
在 laravel 8 app/Exceptions/Handler.php 中只需覆盖方法如下
/**
* 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);
}
Run Code Online (Sandbox Code Playgroud)
就是这样享受
小智 6
“给定的数据无效。” 是硬编码的
文件: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;
Run Code Online (Sandbox Code Playgroud)
从提交:https : //github.com/laravel/framework/pull/22112/commits/b70372fd0031e5fabaee462c913b19b665becaf3
小智 5
抱歉回复晚了,我找到了一个在 Laravel 5.8 及以上版本中工作的方法。\n我希望你仍在寻找答案,这是我的。
\n\n正如 Laravel 官方文档所解释的:
\n\n\n\n\n在 AJAX 请求期间使用该
\nvalidate方法时,Laravel 将不会生成重定向响应。相反,Laravel 会生成包含所有验证错误的 JSON 响应。此 JSON 响应将与 422 HTTP 状态代码一起发送。
因此,您无法翻译响应,因为它是 Laravel 核心的一部分并且是硬编码的,并且在响应中执行相同的操作不是解决方案。
\n\n因此,我建议您对表单使用 Request Validation 方法,然后扩展failedValidation(Validator $validator)作为 FormRequest 类一部分的调用方法。
您可以使用 artisan 控制台创建新的 RequestValidation:\nphp artisan make:request FooRequest
获得FooRequest.php文件后,添加方法failedValidation方法以及建议的行:
/**\n * Handle a failed validation attempt.\n *\n * @param \\Illuminate\\Contracts\\Validation\\Validator $validator\n * @return void\n *\n * @throws \\Illuminate\\Validation\\ValidationException\n */\nprotected function failedValidation(Validator $validator)\n{\n /**\n * @var array $response Is our response data.\n */\n $response = [\n "success" => false, // Here I added a new field on JSON response.\n "message" => __("Los datos enviados no son v\xc3\xa1lidos."), // Here I used a custom message.\n "errors" => $validator->errors(), // And do not forget to add the common errors.\n ];\n\n // Finally throw the HttpResponseException.\n throw new HttpResponseException(response()->json($response, 422));\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我们422回复的结果是:
\n\n我使用 Postman 发送请求,检查响应。\n
\n希望对以后的参考有所帮助。
此致
\n