Javascript Laravel 5 验证 - 作为 json/ajax 返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32555058/
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
Laravel 5 Validation - Return as json / ajax
提问by senty
I am trying to post the values into validation and return the response as json rather than return view
as given in the documentation.
我正在尝试将值发布到验证中并将响应作为 json 而不是return view
文档中给出的返回。
$validator = Validator::make($request->all(), [
'about' => 'min:1'
]);
if ($validator->fails()) {
return response()->json(['errors' => ?, 'status' => 400], 200);
}
The post is made by ajax so I need to receive the response in the ajax as well.
该帖子是由 ajax 制作的,因此我也需要在 ajax 中接收响应。
I figured out that in order to prevent refresh of the page in the returning response, I have to give it a status code of 200 outside the array. But I couldn't figure out what to give the 'errors'
part. What should I write in there?
我发现为了防止在返回的响应中刷新页面,我必须在数组外给它一个 200 的状态代码。但我不知道该给这'errors'
部分什么。我应该在里面写什么?
回答by Peyman.H
You can use $validator->messages()
that returns an array which contains all the information about the validator, including errors. The json
function takes the array and encodes it as a json string.
您可以使用$validator->messages()
它返回一个数组,其中包含有关验证器的所有信息,包括错误。该json
函数接受数组并将其编码为 json 字符串。
if ($validator->fails()) {
return response()->json($validator->messages(), 200);
}
Note: In case of validation errors, It's better not to return response code 200. You can use other status codes like 400 or Response::HTTP_BAD_REQUEST
注意:在验证错误的情况下,最好不要返回响应代码 200。您可以使用其他状态代码,如 400 或Response::HTTP_BAD_REQUEST
回答by Jones03
In Laravel 5.4 the validate()
method can automatically detect if your request is an AJAX request, and send the validator response accordingly.
在 Laravel 5.4 中,该validate()
方法可以自动检测您的请求是否是 AJAX 请求,并相应地发送验证器响应。
See the documentation here
请参阅此处的文档
If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors.
如果验证失败,将生成重定向响应以将用户发送回他们之前的位置。错误也将闪现到会话中,以便它们可供显示。如果请求是 AJAX 请求,则会向用户返回带有 422 状态代码的 HTTP 响应,其中包括验证错误的 JSON 表示。
So you could simply do the following:
所以你可以简单地执行以下操作:
Validator::make($request->all(), [
'about' => 'min:1'
])->validate();
回答by Gjaa
You can also tell Laravel you want a JSON response. Add this header to your request:
你也可以告诉 Laravel 你想要一个 JSON 响应。将此标头添加到您的请求中:
'Accept: application/json'
And Laravel will return a JSON response.
Laravel 会返回一个 JSON 响应。
回答by iman
I believe if you submit an Ajax request you will get a JSON response automatically.
我相信如果您提交 Ajax 请求,您将自动获得 JSON 响应。
Maybe something like this would be appropriate based on your example:
根据您的示例,也许这样的事情是合适的:
$validator = \Validator::make($request->all(), $this->rules());
if ($validator->fails()) {
return response()->json($validator->errors(), 422)
}
回答by Soura Sankar Ghosh
I use below this code for API in my existing project.
我在现有项目中将此代码用于 API。
$validator = Validator::make($request->all(), [
'ride_id' => 'required',
'rider_rating' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
回答by James Mills
My solution is to make my own FormRequest class which I put in the root API namespace namespace App\Http\Requests\Api.
我的解决方案是创建我自己的 FormRequest 类,我将它放在根 API 命名空间命名空间 App\Http\Requests\Api 中。
Hope this helps someone
希望这有助于某人
https://jamesmills.co.uk/2019/06/05/how-to-return-json-from-laravel-form-request-validation-errors/
https://jamesmills.co.uk/2019/06/05/how-to-return-json-from-laravel-form-request-validation-errors/
回答by Mohammad H.
Actually I used @Soura solution but with a little change. You need to import the Validator package as well.
实际上我使用了@Soura 解决方案,但做了一些改动。您还需要导入 Validator 包。
$validator = \Validator::make($request->all(), [
'ride_id' => 'required',
'rider_rating' => 'required',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 400);
}
回答by minhazur
For those who have created a custom request class can override the public function response(array $errors)
method and return a modified response without Validator
explicitly.
对于那些创建了自定义请求类的人,可以覆盖该public function response(array $errors)
方法并返回修改后的响应,而无需Validator
明确。
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\JsonResponse;
class CustomRequest extends FormRequest
{
public function rules()
{
$rules = [
//custom rules
];
return $rules;
}
public function response(array $errors)
{
return new JsonResponse(['error' => $errors], 400);
}
}