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

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

 

今天开始跳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

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

posted @ 2017-09-06 16:12  程序生(Codey)  阅读(1486)  评论(0编辑  收藏  举报