When the laravelAPI interface uses Validator, it cannot return a specific error message that fails verification.

Source: Internet
Author: User
Use Validator in the API to verify the form. if any item passes the verification, {code...} is returned ...} unable 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 does not meet the requirements; I... use Validator in the API to verify the form;
When there are items that pass the verification;
All are returned

{  "message": "The given data failed to pass validation.",  "status_code": 500}

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

{"Message": "The email address has been registered", "status_code": 500} {"message": "The password must be 6-16 characters", "status_code": 500}

Ask your friends. what do you need?

When we use laravel to write interfaces;
How are the submitted form fields verified? How do I return the verification result?

Reply content:

Use Validator in the API to verify the form;
When there are items that pass the verification;
All are returned

{  "message": "The given data failed to pass validation.",  "status_code": 500}

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

{"Message": "The email address has been registered", "status_code": 500} {"message": "The password must be 6-16 characters", "status_code": 500}

Ask your friends. what do you need?

When we use laravel to write interfaces;
How are the submitted form fields verified? How do I return the verification result?

Laravel can customize the error format and must be rewritten in the controller base class.formatValidationErrorsMethod

use Illuminate\Contracts\Validation\Validator;protected function formatValidationErrors(Validator $validator){     $message = $validator->errors()->first();     return ['message'=>$message, 'status_code' => 500];}

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

If you usedingo/apiVerification processing

You can also

If ($ validator-> fails () {// encapsulate a response similar to the following $ this-> respondWithValidatorError ($ validator-> errors ());}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.