Laravel 5.5 FormRequest 自定义错误消息
2017-10-25 16:52
459 查看
Laravel 5.5 FormRequest 自定义错误消息
使用FormRequest进行表单验证,就不用让验证逻辑和控制器里面的逻辑都混在一起。但在使用的时候呢,发现json错误返回的数据,与我们想要的有点差距。下面我给个例子:(不喜勿喷)在用ajax进行提交时,如果验证错了,那么他会返回
如果是权限错了,他会返回
但我想要的是
那怎么办呢,其实很简单
我们只需要在 App\Exceptions\Handler 里面重写两个函数就可以了
添加上这两个函数,然后里面怎么定义,就看你了
记得加上
use Illuminate\Validation\ValidationException; use Illuminate\Auth\Access\AuthorizationException;
最后附上这两个文件的代码
LoginPost
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Auth\AuthenticationException; class LoginPost extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize(){ if($this->input('account')=='aaa@abc.com'){ return false; } return true; } protected function failedAuthorization() { throw new AuthenticationException('该帐号已被拉黑'); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules(){ return [ 'account'=>[ 'required', 'regex:/^1[34578][0-9]\d{4,8}|(\w)+(\.\w+)*@(\w)+((\.\w+)+)|[0-9a-zA-Z_]+$/',//验证为手机号,邮箱,或帐号 ], 'password'=>'required|between:6,18',//验证密码 ]; } public function messages(){ return [ 'account.required' => '帐号不能为空', 'account.regex' => '帐号不合法', 'password.required' => '密码不能为空', 'password.between' => '密码错误', ]; } }
Handler
<?php namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Validation\ValidationException; use Illuminate\Auth\AuthenticationException; class Handler extends ExceptionHandler { /** * A list of the exception types that are not reported. * * @var array */ protected $dontReport = [ // ]; /** * A list of the inputs that are never flashed for validation exceptions. * * @var array */ protected $dontFlash = [ 'password', 'password_confirmation', ]; /** * Report or log an exception. * * This is a great spot to send exceptions to Sentry, Bugsnag, etc. * * @param \Exception $exception * @return void */ public function report(Exception $exception) { parent::report($exception); } /** * Render an exception into an HTTP response. * * @param \Illuminate\Http\Request $request * @param \Exception $exception * @return \Illuminate\Http\Response */ public function render($request, Exception $exception) { return parent::render($request, $exception); } protected function unauthenticated($request, AuthenticationException $exception) { if($request->expectsJson()){ $response=response()->json( 940a [ 'status'=>3, 'msg' => $exception->getMessage(), 'errors'=>[], ], 200); }else{ $response=redirect()->guest(route('login')); } return $response; } protected function invalidJson($request, ValidationException $exception) { return response()->json([ 'status'=>2, 'msg' => $exception->getMessage(), 'errors' => $exception->errors(), ], $exception->status); } }
相关文章推荐
- Laravel 5.5 FormRequest 自定义错误消息
- ASP.NET 4.0事件消息: 发生了验证错误;检测到有潜在危险的 Request.Form 值。
- Laravel5.5在nginx访问自定义路由返回404错误
- Laravel中使用FormRequest进行表单验证及对验证异常进行自定义处理
- ASP.NET 4.0事件消息: 发生了验证错误;检测到有潜在危险的 Request.Form 值。
- laravel自定义错误页面
- Yii自定义错误提示消息
- Laravel5.5以下版本中如何自定义日志行为详解
- laravel5.5 服务(一) 用户认证简单自定义
- Laravel中使用FormRequest进行表单验证方法及问题汇总
- 解决asp.net中“从客户端(......)中检测到有潜在危险的 Request.Form 值。”错误
- laravel自定义错误页面
- C#WinForm下自定义错误消息
- 访问MS CRM 自定义web service 碰到的错误: FIX Request format is unrecognized for URL unexpectedly ending in
- asp.net错误信息(检测到有潜在危险的 Request.Form)
- 错误: Sys.WebForms.PageRequestManagerParserErrorException: 未能分析从服务器收到的消息。
- 客服端调用自定义宿主的WCF报错"没有终结点在侦听可以接受消息的http://localhost:8085/mex。这通常是由于不正确的地址或者 SOAP 操作导致的错误"的解决方案。
- 出现从客户端中检测到有潜在危险的 Request.Form 值的错误时的解决办法
- 错误提示 从客户端(Content="<br>测试")中检测到有潜在危险的 Request.Form 值。
- 解决asp.net中“从客户端(......)中检测到有潜在危险的 Request.Form 值。”错误