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.formatValidationErrors
Method
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/api
Verification processing
You can also
If ($ validator-> fails () {// encapsulate a response similar to the following $ this-> respondWithValidatorError ($ validator-> errors ());}