gth*_*huo 2 validation laravel-5
我正在使用L5表格请求,我不喜欢泰勒!好吧,我正在做一些AJAX请求,我仍然想保留我的表单请求.问题是,在验证错误的情况下,Validator只返回422错误响应并闪烁错误,但我的AJAX前端需要服务器的非常特定的响应格式,无论验证是否成功.
我想将验证错误的响应格式化为这样的
return json_encode(['Result'=>'ERROR','Message'=>'//i get the errors..no problem//']);
Run Code Online (Sandbox Code Playgroud)
我的问题是如何格式化表单请求的响应,特别是当这不是全局但在特定表单请求上完成时.
我用谷歌搜索,但没有看到非常有用的信息.在深入研究Validator课程后也尝试过这种方法.
// added this function to my Form Request (after rules())
public function failedValidation(Validator $validator)
{
return ['Result'=>'Error'];
}
Run Code Online (Sandbox Code Playgroud)
仍然没有成功.
Rag*_*gas 10
目前接受的答案不再有效,所以我给出了更新的答案.
在虔诚的FormRequest使用failedValidation功能中抛出一个自定义exception
// use Illuminate\Contracts\Validation\Validator;
// use App\Exceptions\MyValidationException; at top
protected function failedValidation(Validator $validator)
{
throw new MyValidationException($validator);
}
Run Code Online (Sandbox Code Playgroud)
在中创建自定义例外 app/Exceptions
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Contracts\Validation\Validator;
class MyValidationException extends Exception
{
protected $validator;
protected $code = 422;
public function __construct(Validator $validator)
{
$this->validator = $validator;
}
public function render()
{
// return a json with desired format
return response()->json([
"error" => "form validation error",
"message" => $this->validator->errors()->first()
], $this->code);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我找到的唯一方法.如果有更好的方法,请发表评论.
这在laraval5.5中有效,我认为这不会在laravel5.4中有效,但我不确定.
如果您使用 laravel 5+,您可以通过覆盖文件中的invalid()orinvalidJson()方法轻松实现此目的App/Exceptions/Handler.php
就我而言,我正在开发一个 API,并且 API 响应应该采用特定格式,因此我在文件中添加了以下内容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([
'code' => $exception->status,
'message' => $exception->getMessage(),
'errors' => $this->transformErrors($exception),
], $exception->status);
}
// transform the error messages,
private function transformErrors(ValidationException $exception)
{
$errors = [];
foreach ($exception->errors() as $field => $message) {
$errors[] = [
'field' => $field,
'message' => $message[0],
];
}
return $errors;
}
Run Code Online (Sandbox Code Playgroud)
在这里找到答案:Laravel 5 自定义验证重定向
您所需要做的就是response()在表单请求中添加一个方法,它将覆盖默认响应。在您中,response()您可以按照您想要的任何方式进行重定向。
public function response(array $errors)
{
// Optionally, send a custom response on authorize failure
// (default is to just redirect to initial page with errors)
//
// Can return a response, a view, a redirect, or whatever els
return response()->json(['Result'=>'ERROR','Message'=>implode('<br/>',array_flatten($errors))]); // i wanted the Message to be a string
}
Run Code Online (Sandbox Code Playgroud)
L5.5+ 上的更新
此错误和接受的解决方案适用于 L5.4。对于 L5.5,使用上面 Ragas 的答案(failedValidation() 方法)