1 Star 1 Fork 0

xingfupeng / laravel-super-validator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

laravel-super-validator

组件介绍

在 Laravel 开发过程中,你是否会因为过多的参数校验而苦恼。laravel-super-validation 会帮助你实现场景参数校验、公共参数校验或者智能参数校验模式。从来轻松解决参数校验的苦恼。而且还可以达到很好的解耦效果。

** 重点 ** : 控制器里不需要写任何参数校验代码也能实现参数校验。

校验思想

组件内置很多通用字段校验,默认规则非必填,但会按照既定的格式进行校验。
当客户端传递过来的时候,会根据预设的判定规则进行判断,没有传递时参数时不进行判定。

例如 : id 校验规则必须是整数。默认情况下客户端不传递参数则不校验。当客户端传递 id 字段的时候则按照整数的规则校验。
email 同理当客户端传递 email 字段便会按照邮箱的格式校验,否则不校验。
这样一来在控制器层面不需要写过多的参数校验。

组件支持场景校验,当用户用邮箱登录的时候,场景中定义邮箱字段为必填,与内置的校验规则合并一起校验。
场景设定的规则是控制器的命名空间类名加方法名。
例如 : App\Http\Controllers\ValidationController@index
自动校验 App\Http\Controllers\ValidationController.php 文件下的 index 方法

安装教程

  1. 安装
    composer require xingfupeng/laravel-super-validator
  2. 发布配置文件
    php artisan vendor:publish --tag=config --force
  3. 添加服务器提供者配置
    // config/app.php
    'providers' => [
        ...
        Xingfupeng\LaravelSuperValidator\Providers\LaravelSuperValidatorProvider::class,
    
    ],

使用说明

  1. 参数配置
    config/laravel_super_validator.php 的配置项可以覆盖 laravel_super_validator中的配置项 config/laravel_super_validator_fields.php 通用检验配置
  2. 自定义校验异常操作
    捕获异常信息后可自定义数据格式返回给客户端,特别使用于接口开发。
    // app/Exceptions/Handler.php
    public function register()
    {
        $this->reportable(function (LarvelSuperValidatorException $e) {
            return response($e->getMessage());
        });
        ...
    }
  3. 通用配置校验
     // config/laravel-super-validator-fields.php
     <?php
     return [
         'id' => [
             'rules' => 'integer',
             'messages' => [
                 'required' => '请输入ID',
                 'integer' => 'ID必须是数字',
             ]
         ],
         'page' => [
             'rules' => 'integer',
             'messages' => [
                 'required' => '请输入分页',
                 'integer' => '页数必须是数字',
             ]
         ],
     ];
  4. 场景校验配置
     // config/laravel-super-validator-scenes.php
     <?php
     return [
         'App\Http\Controllers\ValidationController@index' => [
             'id' => [
                 'rules' => 'required|min:3|max:6',
                 'messages' => [
                     'required' => 'ID不能为空',
                     'min' => 'id不能少于:min个字符',
                     'max' => 'id不能多于:max个字符'
                 ]
             ],
             'page' => [
                 'rules' => 'required',
             ]
         ],
     ];

内置校验

return [
    'id' => [                              // 字段名称
        'rules' => 'integer',              // 字段规则
        'messages' => [                    // 字段校验信息
            /**
             * 这里需要注意的
             * 每一个字段都会有一个必填信息,
             * 当场景校验添加 required 校验规则,
             * 场景中设置了 message 会覆盖这里, 
             * 否则默认就是显示这里的信息提示。
             */ 
            'required' => '请输入ID',       
            'integer' => 'ID必须是数字',
        ]
    ],
    'page' => [
        'rules' => 'integer',
        'messages' => [
            'required' => '请输入分页',
            'integer' => '页数必须是数字',
        ]
    ],
];
MIT License Copyright (c) 2022 xingfupeng Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

在 Laravel 开发过程中,你是否会因为过多的参数校验而苦恼。laravel-super-validation 会帮助你实现场景参数校验、公共参数校验或者智能参数校验模式。从来轻松解决参数校验的苦恼。而且还可以达到很好的解耦效果。 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/xingfupeng/laravel-super-validator.git
git@gitee.com:xingfupeng/laravel-super-validator.git
xingfupeng
laravel-super-validator
laravel-super-validator
master

搜索帮助