The laravel API interface uses Validator and cannot return specific error information that fails verification.

WBOY
Release: 2016-10-19 10:40:49
Original
2025 people have browsed it

Validator is used in the API interface to verify the form;
When there are items that pass verification;
are all returned

<code>{
  "message": "The given data failed to pass validation.",
  "status_code": 500
}</code>
Copy after login
Copy after login

It is impossible to return specific information that has not passed verification;
When the front-end or mobile terminal receives the returned error message;
It cannot tell the user which item does not meet the requirements;
What I want is this;

<code>{
  "message": "邮箱已经注册",
  "status_code": 500
}

{
  "message": "必须是6-16位的密码",
  "status_code": 500
}</code>
Copy after login
Copy after login

Dear friends, what should I do?

Dear friends, when writing interfaces with laravel;
How are the submitted form fields verified? How to return verification results?

Reply content:

Validator is used in the API interface to verify the form;
When there are items that pass verification;
are all returned

<code>{
  "message": "The given data failed to pass validation.",
  "status_code": 500
}</code>
Copy after login
Copy after login

It is impossible to return specific information that has not passed verification;
When the front-end or mobile terminal receives the returned error message;
It cannot tell the user which item does not meet the requirements;
What I want is this;

<code>{
  "message": "邮箱已经注册",
  "status_code": 500
}

{
  "message": "必须是6-16位的密码",
  "status_code": 500
}</code>
Copy after login
Copy after login

Dear friends, what should I do?

Dear friends, when writing interfaces with laravel;
How are the submitted form fields verified? How to return verification results?

Laravel can customize the error format, which needs to be rewritten in the controller base class formatValidationErrors method

<code>use Illuminate\Contracts\Validation\Validator;

protected function formatValidationErrors(Validator $validator)
{
     $message = $validator->errors()->first();
     return ['message'=>$message, 'status_code' => 500];
}</code>
Copy after login

You can also expand it according to your own needs. The above is just a simple example, and the first error message is returned.

If you use dingo/api verification handler

Of course you can also

<code>if ($validator->fails()) {
     //自行封装个处理验证失败返回值 类似下面
     $this->respondWithValidatorError($validator->errors());    
}
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template