Laravel 5.5 FormRequest 自定义错误消息

2024-02-17 09:32

本文主要是介绍Laravel 5.5 FormRequest 自定义错误消息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Laravel 5.5 FormRequest 自定义错误消息

  使用FormRequest进行表单验证,就不用让验证逻辑和控制器里面的逻辑都混在一起。但在使用的时候呢,发现json错误返回的数据,与我们想要的有点差距。下面我给个例子:(不喜勿喷) 
  这里写图片描述

  在用ajax进行提交时,如果验证错了,那么他会返回 
  这里写图片描述 
   
  如果是权限错了,他会返回 
  这里写图片描述

  但我想要的是 
  这里写图片描述

  那怎么办呢,其实很简单 
  我们只需要在 App\Exceptions\Handler 里面重写两个函数就可以了 
  这里写图片描述
  添加上这两个函数,然后里面怎么定义,就看你了 
  记得加上 
  

       use Illuminate\Validation\ValidationException;use Illuminate\Auth\Access\AuthorizationException;
  • 1
  • 2

最后附上这两个文件的代码 
LoginPost

<?phpnamespace 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'  => '密码错误',];}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

Handler

<?phpnamespace 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(['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 自定义错误消息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/717366

相关文章

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

SpringIntegration消息路由之Router的条件路由与过滤功能

《SpringIntegration消息路由之Router的条件路由与过滤功能》本文详细介绍了Router的基础概念、条件路由实现、基于消息头的路由、动态路由与路由表、消息过滤与选择性路由以及错误处理... 目录引言一、Router基础概念二、条件路由实现三、基于消息头的路由四、动态路由与路由表五、消息过滤

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Python中ModuleNotFoundError: No module named ‘timm’的错误解决

《Python中ModuleNotFoundError:Nomodulenamed‘timm’的错误解决》本文主要介绍了Python中ModuleNotFoundError:Nomodulen... 目录一、引言二、错误原因分析三、解决办法1.安装timm模块2. 检查python环境3. 解决安装路径问题

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现