lumen手记:自定义Validate表单验证

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

 

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

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

所有开始搞起,

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

1
$this->validate($request, $rules, $message, $attributes);

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
if ($validator->fails()) {
     /**
       * 修改验证返回
       */
     return $this->formatValidationErrors($validator);
     $this->throwValidationException($request, $validator);
}

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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了?

1
2
3
4
5
6
7
8
9
10
11
12
public function validates(Request $request, array $rules, array $messages = [], array $customAttributes = [])
    {
        $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);
     
        if ($validator->fails()) {<br>       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

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
 
namespace App\Http\Controllers;
 
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Providers\Validate\AppProvidersValidate;
 
class Controller extends BaseController
{
    //全局表单验证定制类
    use AppProvidersValidate;
    //
}
1
AppProvidersValidate.php放在哪,在什么命名空间下,你开心就好咯!我放在app\Providers\Validate\AppProvidersValidate.php具体代码如下,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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()函数的实现位置,喜欢可以研究探讨下

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
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

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

posted @   程序生(Codey)  阅读(1492)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示