Laravel 5.5 formrequest Custom error message
By using Formrequest for form validation, you don't have to mix the validation logic with the logic inside the controller. But in the use of the time, found that the JSON error returned data, and we want a little gap. Below I give an example: (Do not like to spray)
When committing with Ajax, if the validation is wrong, then he will return
If the permission is wrong, he will return.
But what I want is
What about it, it's really simple.
We just need to rewrite two functions in App\exceptions\handler.
Add these two functions, and then how do you define them?
Remember to add
use Illuminate\Validation\ValidationException; use Illuminate\Auth\Access\AuthorizationException;
Finally, I enclose the code for these two files.
Loginpost
<?phpNamespaceApp\Http\Requests;UseIlluminate\foundation\Http\Formrequest;UseIlluminate\Auth\Authenticationexception;ClassLoginpostExtendsformrequest{/** * Determine if the user is authorized to make this request. * *@return BOOL */PublicfunctionAuthorize(){If$this->input (' account ') = =' [email protected] ' {ReturnFalse }ReturnTrue }ProtectedfunctionFailedauthorization() {ThrowNew Authenticationexception (' The account has been pulled black '); }/** * Get The validation rules that apply to the request. * *@return Array */PublicfunctionRules(){return [' Account ' =>[' Required ',' regex:/^1[34578][0-9]\d{4,8}| (\w) + (\.\w+) *@ (\w) + ((\.\w+) +) | [0-9a-za-z_]+$/',//verified as mobile number, mailbox, or account number], ' password ' = 'required|between:6,18 ',//verify password];} public function messages() { return [ ' account.required ' = ' account cannot be empty ', ' Account.regex ' + ' account is not legal ', ' password.required ' = ' password cannot be empty ', ' password.between ' = ' Password error ',]; }
Handler
<?phpNamespaceApp\Exceptions;UseException;UseIlluminate\foundation\Exceptions\HandlerAsExceptionhandler;UseIlluminate\validation\Validationexception;UseIlluminate\Auth\Authenticationexception;ClassHandlerExtendsexceptionhandler{/** * A List of the exception types that is not reported. * *@var Array */Protected$dontReport = [// ];/** * A List of the inputs that is never flashed for validation exceptions. * *@var Array */Protected$dontFlash = [' Password ',' Password_confirmation ',];/** * Report or log an exception. * * This was a great spot to send exceptions to Sentry, Bugsnag, etc. * *@param \exception $exception *@return void */PublicfunctionReport(Exception$exception) {Parent::report ($exception); }/** * Render An exception to an HTTP response. * *@param \illuminate\http\request $request *@param \exception $exception *@return \illuminate\http\response * *PublicfunctionRender($request, Exception$exception) {ReturnParent::render ($request,$exception); }ProtectedfunctionUnauthenticated($request, Authenticationexception$exception) {If$request->expectsjson ()) {$response =response ()->json ([' Status ' =3,' Msg ' =$exception->getmessage (),' Errors ' =>[],],200); }else{ $response =redirect ()->guest (Route (return $response;} protected function invalidjson ( $request, validationexception Span class= "hljs-variable" > $exception) {return response ()->json ([ ' status ' =>2, ' msg ' = $exception->getmessage (), ' errors ' = $exception->errors (),], $exception->status); }}
---------------------This article from the CSDN blog of know water flow, full-text address please click: 78343408?utm_source=copy
Laravel 5.5 formrequest Custom error message