zoukankan      html  css  js  c++  java
  • lumen手记:自定义Validate表单验证

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

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

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

    所有开始搞起,

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

    $this->validate($request, $rules, $message, $attributes);
    
    namespace LaravelLumenRouting;
    
    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);
    }
    

    重新发送请求,果然生效了!!!可是...   看了一下路径vendorlaravellumen-frameworksrcRoutingProvidesConvenienceMethods.php,尴尬了,是扩展包的类。。。

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

    <?php
    
    namespace LaravelLumenRouting;
    
    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吧——appHttpController/Controller.php

    <?php
    
    namespace AppHttpControllers;
    
    use LaravelLumenRoutingController as BaseController;
    use AppProvidersValidateAppProvidersValidate;
    
    class Controller extends BaseController
    {
        //全局表单验证定制类
        use AppProvidersValidate;
        //
    }
    
    AppProvidersValidate.php放在哪,在什么命名空间下,你开心就好咯!我放在appProvidersValidateAppProvidersValidate.php具体代码如下,
    <?php
    namespace AppProvidersValidate;
    
    use IlluminateHttpRequest;
    
    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

    补充一下:

    vendorilluminatevalidationFactory.php是make()函数的实现位置,喜欢可以研究探讨下

    namespace IlluminateValidation;
    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

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

  • 相关阅读:
    ibatis命名空间namespace的使用
    MyEclipse 下新建Flex与JAVA交互项目
    第2章 TCP/IP 和Internet
    第一部分:TCP/IP 基础 第一章 开放式通信模型简介
    01-布局扩展-利用盒模型完成圣杯布局(双飞翼布局)
    01-布局扩展-用calc来计算实现双飞翼布局
    01-布局扩展-BFC完成圣杯布局
    Nginx
    uni-app mpvue wepy websocket的介绍
    taro 使用taro中的vue来完成小程序的开发
  • 原文地址:https://www.cnblogs.com/cxscode/p/7485379.html
Copyright © 2011-2022 走看看