版权声明:本文为博主原创文章,未经博主允许不得转载。

今天开始跳lumen的表单验证Validate类的坑,确实好坑!!!

首先,lumen的表单验证返回是无状态的json格式api,这...

所有开始搞起,

先来看看官方的方法,验证不通过直接返回json。

$this->validate($request, $rules, $message, $attributes);
namespace Laravel\Lumen\Routing;

trait ProvidesConvenienceMethods{

......

    public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
{
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); if ($validator->fails()) {
$this->throwValidationException($request, $validator);
}
} ...... }

 

$this->throwValidationException($request, $validator);原来是这里直接返回json

if ($validator->fails()) {
/**
* 修改验证返回
*/
return $this->formatValidationErrors($validator);
$this->throwValidationException($request, $validator);
}

重新发送请求,果然生效了!!!可是...   看了一下路径vendor\laravel\lumen-framework\src\Routing\ProvidesConvenienceMethods.php,尴尬了,是扩展包的类。。。

控制器的底层类Controller.php也在这里,点进去look一look,

<?php

namespace Laravel\Lumen\Routing;

class Controller
{
use ProvidesConvenienceMethods; /**
* The middleware defined on the controller.
*
* @var array
*/
protected $middleware = []; /**
* Define a middleware on the controller.
*
* @param string $middleware
* @param array $options
* @return void

果然在这里引用ProvidesConvenienceMethods,而前面控制器的$this->validate应该也是调的这里,一切明了,在这里加一个和validate类似的方法不就OK了?

public function validates(Request $request, array $rules, array $messages = [], array $customAttributes = [])
{
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); if ($validator->fails()) {
       echo 1;exit();
/**
* 修改验证返回
*/
return $this->formatValidationErrors($validator);
//$this->throwValidationException($request, $validator);
}
}

调用$this->validates($request, $rules, $message, $attributes);//输出1

注释断点再测试,发现验证通过,返回结果集!

不过写在这里肯定不好,那就写在比较靠近应用层的Controller.php吧——app\Http\Controller/Controller.php

<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use App\Providers\Validate\AppProvidersValidate; class Controller extends BaseController
{
//全局表单验证定制类
use AppProvidersValidate;
//
}
AppProvidersValidate.php放在哪,在什么命名空间下,你开心就好咯!我放在app\Providers\Validate\AppProvidersValidate.php具体代码如下,
<?php
namespace App\Providers\Validate; use Illuminate\Http\Request; trait AppProvidersValidate
{
public function validates(Request $request, array $rules, array $messages = [], array $customAttributes = [])
{
$validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes); if ($validator->fails()) {
/**
* 修改验证返回
*/
return $this->formatValidationErrors($validator);
//$this->throwValidationException($request, $validator);
}
}
}

以后一些针对表单验证的处理操作也可以放在这里啦!!!网上资料好少,纯手打,请不要转载,谢谢啦!!!详细分析(添加手机验证,中文验证与Validator验证的“半个”生命周期):http://www.cnblogs.com/cxscode/p/7561277.html

补充一下:

vendor\illuminate\validation\Factory.php是make()函数的实现位置,喜欢可以研究探讨下

namespace Illuminate\Validation;
class Factory implements FactoryContract {   ...... public function make(array $data, array $rules, array $messages = [], array $customAttributes = [])
{
// The presence verifier is responsible for checking the unique and exists data
// for the validator. It is behind an interface so that multiple versions of
// it may be written besides database. We'll inject it into the validator.
$validator = $this->resolve(
$data, $rules, $messages, $customAttributes
); if (! is_null($this->verifier)) {
$validator->setPresenceVerifier($this->verifier);
} // Next we'll set the IoC container instance of the validator, which is used to
// resolve out class based validator extensions. If it is not set then these
// types of extensions will not be possible on these validation instances.
if (! is_null($this->container)) {
$validator->setContainer($this->container);
} $this->addExtensions($validator); return $validator;
} ...... }

补充,如果找不到想要的验证:

https://www.cnblogs.com/tfcwolf/p/4350283.html

本文地址:http://www.cnblogs.com/cxscode/p/7485379.html

版权声明:本文为博主原创文章,未经博主允许不得转载。

lumen手记:自定义Validate表单验证的更多相关文章

  1. ASP.NET MVC Jquery Validate 表单验证的多种方式

    在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体验也会得到很大的提升.在开发过程中我们可以不借助 JS 库,自己去手写 JS ...

  2. 【干货】Laravel --Validate (表单验证) 使用实例

    前言 : Laravel 提供了多种方法来验证应用输入数据.默认情况下,Laravel 的控制器基类使用ValidatesRequests trait,该trait提供了便利的方法通过各种功能强大的验 ...

  3. [转]ASP.NET MVC Jquery Validate 表单验证的多种方式介绍

    在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体验也会得到很大的提升.在开发过程中我们可以不借助 JS 库,自己去手写 JS ...

  4. Jquery Validate 表单验证的多种方式

    ASP.NET MVC Jquery Validate 表单验证的多种方式 在我们日常开发过程中,前端的表单验证很重要,如果这块处理不当,会出现很多bug .但是如果处理的好,不仅bug会很少,用户体 ...

  5. jQuery-easyui和validate表单验证实例

    jQuery EasyUI 表单 - 表单验证插件validatebox 使用时需要向页面引入两个css文件如下: <link rel="stylesheet" href=& ...

  6. 看用Tornado如何自定义实现表单验证

    我们知道,平时在登陆某个网站或软件时,网站对于你输入的内容是有要求的,并且会对你输入的错误内容有提示,对于Django这种大而全的web框架,是提供了form表单验证功能,但是对于Tornado而言, ...

  7. 基于jQuery的Validate表单验证

    表单验证可以说在前端开发工作中是无处不在的~ 有数据,有登录,有表单, 都需要前端验证~~  而我工作中用到最多的就是基于基于jQuery的Validate表单验证~  就向下面这样~ 因为今天有个朋 ...

  8. summernote富文本编辑器配合validate表单验证无法进行表单提交的问题

    1.使用summernote富文本编辑器提交图片到服务器 在使用bootstrap中,我们用到了summernote富文本编辑器,使用summernote将图片上传到服务器中,参考我的上篇文章http ...

  9. thinkphp图片上传+validate表单验证+图片木马检测+缩略图生成

    目录 1.案例 1.1图片上传  1.2进行图片木马检测   1.3缩略图生成   1.4控制器中调用缩略图生成方法 1.案例 前言:在thinkphp框架的Thinkphp/Library/Thin ...

随机推荐

  1. zookeeper 学习笔记1(转)

    本文转自https://www.cnblogs.com/fanguangdexiaoyuer/p/7077520.html 感谢作者 可以设置观察的操作:exists,getChildren,getD ...

  2. 获取textview行数

    获取textview行数 textview 代码 import android.content.Context; import android.graphics.Canvas; import andr ...

  3. 上机题目(0基础)- Java网络操作-Socket实现client和server端通信二(Java)

    上一节实现了client像server端发送请求.本节将实现server端向client回传信息.实现原理非常easy,在原来的基础上.在server端实现输出流,在client实现输入流就可以,详细 ...

  4. OpenGL帧缓存对象(FBO:Frame Buffer Object) 【转】

    http://blog.csdn.net/dreamcs/article/details/7691690 原文地址http://www.songho.ca/opengl/gl_fbo.html 但有改 ...

  5. .net 真实代理和透明代理的交互

    .本地代理调用 using System; using System.Runtime.Remoting ; using System.Runtime.Remoting.Services ; using ...

  6. [Apollo Server] Get started with Apollo Server

    Get started with apollo server with node.js: Install: npm install --save apollo-server graphql index ...

  7. javaweb项目自定义错误页面

    当我们把一个web项目成功发布出去,但是有些页面还有待完善的时候,会出现404错误页面.这个会给用户很差的体验.如何将这些错误页面修改为自定义的错误页界面,给用户一些友好的提示呢? 首先我们在web. ...

  8. functools.wraps

    我们在使用 Decorator 的过程中,难免会损失一些原本的功能信息.直接拿 stackoverflow 里面的栗子     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  9. defer,panic,recover

    Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常,甚至一个小小的错误都抛出一个 ...

  10. 单点登录cas常见问题(四) - ticket有哪些存储方式?

    配置文件ticketRegistry.xml负责配置ticket的存储方式,registry是注冊表,登记薄的意思 经常使用的存储方式包含 1.DefaultTicketRegistry:默认的.存储 ...