PHPackages                             youloge/webman.validate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. youloge/webman.validate

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

youloge/webman.validate
=======================

Webman plugin youloge/webman.validate

v2.0.2(1mo ago)392MITPHP

Since Mar 16Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/youfeed/webman.validate)[ Packagist](https://packagist.org/packages/youloge/webman.validate)[ RSS](/packages/youloge-webmanvalidate/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)DependenciesVersions (16)Used By (0)

Youloge.validate Webman 高级表单验证器(PHP7.2+)
========================================

[](#youlogevalidate-webman-高级表单验证器php72)

[![Brightgreen](https://camo.githubusercontent.com/48b2d4e76111b1691060c27bb1583c7418455eb0e8d47c7344ae1c13d6c763fd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f402d6d6963617465616d2d627269676874677265656e2e737667)](https://camo.githubusercontent.com/48b2d4e76111b1691060c27bb1583c7418455eb0e8d47c7344ae1c13d6c763fd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f402d6d6963617465616d2d627269676874677265656e2e737667) [![Packagist](https://camo.githubusercontent.com/784261123d6bf939048de6ca49440ce2a81fa3ced081b08b7eb8343c8218e634/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796f756c6f67652f7765626d616e2e76616c6964617465)](https://camo.githubusercontent.com/784261123d6bf939048de6ca49440ce2a81fa3ced081b08b7eb8343c8218e634/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f796f756c6f67652f7765626d616e2e76616c6964617465) [![Languages](https://camo.githubusercontent.com/43abb45fa7ccf612e050e629254e3e8c62d711dd92762c78cafaa171dd85f2f3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f796f75666565642f7765626d616e2e76616c69646174652e737667)](https://camo.githubusercontent.com/43abb45fa7ccf612e050e629254e3e8c62d711dd92762c78cafaa171dd85f2f3/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f746f702f796f75666565642f7765626d616e2e76616c69646174652e737667) [![Packagist Downloads](https://camo.githubusercontent.com/a1b5d96f6d847687c99cbc59d30426ef69e77f29258bb8f2b842671c4b7ca7fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796f756c6f67652f7765626d616e2e76616c6964617465)](https://camo.githubusercontent.com/a1b5d96f6d847687c99cbc59d30426ef69e77f29258bb8f2b842671c4b7ca7fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f796f756c6f67652f7765626d616e2e76616c6964617465) [![License ](https://camo.githubusercontent.com/0428f4f03fe39f0c5bca539cf064b54bbf91e17323d2c15a7584861d1241e8bb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f796f756c6f67652f7765626d616e2e76616c6964617465)](https://camo.githubusercontent.com/0428f4f03fe39f0c5bca539cf064b54bbf91e17323d2c15a7584861d1241e8bb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f796f756c6f67652f7765626d616e2e76616c6964617465)

> Webman 表单验证器是：[Github Youloge.Tool](https://github.com/youfeed/webman.tool) 的子函数之一，如果安装了`webman.tool`就不需要再安装`webman.validate`了。经过`webman.validate` 过滤处理后的表单，基本可以达到入库要求，可以大大简化逻辑处理。\[2.0.0+ 版本进行了重写 现在处理可以处理更加复杂的表单且不需要进行多次验证，在一个验证规则中即可处理任意复杂度的数据\]

- \[新\]支持自定义匿名函数处理
- \[新\]支持数据流式流转处理
- 支持数据链式预处理处理
- 支持`对象`和`数组`数据验证
- 支持自定义错误提示
- 支持`字段过滤`，仅返回指定验证字段
- 可以对任意复杂表单进行处理过滤转化并合并结果

### 项目地址

[](#项目地址)

[Youloge.Validate](https://github.com/youfeed/webman.validate) Star 我 `如果对你的项目有帮助` 欢迎打赏~

- 2.0.1 \[2026-03-07\] 新增`流式处理 [M,N,Q]`,新增`callable闭包`自定义处理函数
- 1.4.2 \[2025-11-11\] 新增`sprintf`,`format`规则，格式化字符串
- 1.4.0 \[2025-10-25\] 优化`required`规则，允许`0、'0'、false、[]、{}`
- 1.3.3 \[2025-08-14\] 新增`array`和`object`数据类型
- 1.3.1 \[2025-08-12\] 默认数据类型与默认值：数据类型修复为一致
- 1.3.0 \[2025-04-23\] 添加`array_is_list`函数，兼容到 php7.2+
- 1.2.8 \[2025-03-20\] 基本数据类型`int:100`,`float:1.02`,`bool:false`,`string:默认值` 提供默认值支持

### 安装使用

[](#安装使用)

> `composer require youloge/webman.validate`

> 函数名称 `useValidate()`

### 使用说明

[](#使用说明)

验证规则: `|`,`:`,`,`,`#`,`[]`

- `|` 分割多个规则
- `:` 规则参数 `,`多个参数用逗号分隔
- `#` 自定义错误提示
- $return = useValidate($params,$rules,$filter=true)
- `$params` 验证数据
- `$rules` 验证规则
- `$filter` 是否过滤数据,剔除规则之外的数据在返回 默认 true
- 验证失败返回 `['err'=>400,'msg'=>'错误提示']`
- 验证成功返回 处理过后的数据`(filter)$params`

### 参数说明

[](#参数说明)

验证规则说明示例规则类型required字段必填required基本处理int转为 int 类型int:10基本处理bool转为 bool 类型bool:false基本处理float转为 float 类型float:1.2基本处理string转为 string 类型int:string基本处理array转为 array 类型array:\[\]基本处理object转为 object 类型object:{}基本处理upper转为 大写字母upper基本处理lower转为 小写字母lower基本处理sprintf格式化字符串sprintf:%.2f基本处理format格式化字符串sprintf:%.4f基本处理join数组转字符串：可以指定连接符号，默认逗号join:@基本处理trim去除参数两端空白符trim基本处理xss过滤特殊符号:支持不过滤(标签)xss:基本处理html转义 HTML 标签html基本处理------------email验证是否为邮箱email常用验证mobile验证是否手机号(国内)mobile常用验证url验证是否 URL 协议网址url常用验证ip验证是否 IP 地址ip常用验证date验证日期date:Y-m-D H:i:s常用验证idcard验证是否身份证(国内)idcard常用验证regex正则验证regex:\[a-zA-Z\]{16}常用验证test正则验证regex:\[a-zA-Z\]{16}常用验证------------min参数最小值min:1数字相关max参数最大值max:20数字相关between参数取值范围between:1,100数字相关------------start参数必须以%开头start:@字符相关end参数必须以%结尾end:@qq.com字符相关digit参数必须为纯数字digit字符相关alpha参数必须为纯字母alpha字符相关alphanum参数必须为数字和数字alphanum字符相关length参数字符长度要在 min ～ max 之间length:12,14字符相关len参数字符长度要在 min ～ max 之间length:12,14字符相关------------in参数取值在之内between:a,b,c取值相关not参数取值在之外between:x,y,z取值相关count数组个数要在 min ～ max 之间count:1,6取值相关> xss 过滤不仅会过滤 html 标签，还会过滤 `'`,`"`,`;`,`--`,`%`,`_`,`(`,`)`, 可以防止`Mysql 注入`

---

- =============================
- = 过滤规则分为`预处理`和`验证规则` 新增`闭包函数`
- =============================
- = `基本处理:` required int bool float string join trim upper lower sprintf format xss html
- = `常用验证:` email mobile url ip date time idcard regex test
- = `数字相关:` min max between
- = `字符相关:` start end digit alpha alphanum length
- = `取值相关:` in not count
- = `闭包函数:` function($field,$value){}
- =============================

---

### 简单示例：

[](#简单示例)

```
$params = [
     'username' => 'admin',
     'password' => '123456',
];
$rules =[
     'username' => 'required|trim|lower|email', // 必填，去除空格，转小写，邮箱格式
     'password' => function($field,$value){
          // 验证不通过返回 `Exception` 即可
          if(count($value) < 8){
               throw new Exception("密码长度不足~");
          }
          // 验证通过 并处理数据
          return md5($value);
     }
];

@['err'=>$err,'msg'=>$msg] = $data = useValidate($params,$rules,$filter=true);
if($err === 400) return json(['err'=>400,'msg'=>$msg]);
```

### 复杂示例：

[](#复杂示例)

##### 请求数据：

[](#请求数据)

```
{
  "title": "文章标题",
  "content": "文章内容",
  "type": 3,
  "status": "00000001",
  "share": 1,
  "price": "a12.34",
  "created": "2022-01-01 12:00:00",
  "tags": ["标签1", "标签2"],
  "info": {
    "origin": "https://www.youloge.com",
    "ip": "127.0.0.1"
  },
  "list": [
    {
      "name": "张三",
      "age": 18,
      "mail": "EMAIL100@qq.com"
    },
    {
      "name": "李四",
      "age": 20
    },
    {
      "name": "李四",
      "age": 20
    }
  ]
}
```

#### 验证规则：

[](#验证规则)

```
$rules =[
     // length:64,6,30 (不是书写错误 不区分变量数量 不区分变量前后)
     'title' => 'required|xss|length:64,6,30', // 必填，长度6-64 去除html去除 HTML 和 PHP 标签
     'content' => 'required|html', // 必填，转义html标签
     'type' =>'required|in:1,2,3', // 必填，只能是1,2,3,不能是其他值
     'status' => 'required|int|not:0,99,100', // 必填，并转换成整数，不能是0,99,100
     'share' => 'required|bool', // 必填，并转换成布尔值
     'price' => 'required|float|format:%.2f|max:100', // 必填，并转换成浮点数 并格式化保留2位小数 最大100
     'created' => 'required|date:Y-m-d H:i', // 必填，日期格式(默认为：Y-m-d H:i:s)
     // 'tags' => 'required|count:1,6', // 必填，需要1~6个标签 [`相同键 只生效最后一个哦`]
     // 'tags' =>['required|length:2,20'], //  数组的每个值需要2-20个字符 [`相同键 只生效最后一个哦`]
     // 'tags' =>'required|join:-', // 使用-符号连接 [`相同键 只生效最后一个哦`]
     // 当验证规则是数组且不止一个规则则可以写成流式处理(将上面针对tags的规则组成一个规则)
     'tags'=>['required|count:1,6',['required|length:2,20','required|join:@']],
     // 注意这个规则是[数组对象]，所以下面的规则会循环验证数组内的对象
     'list' => [
          [
               'name'=>'required|string', // 必填，并转换成字符串
               'age'=>'required|int', // 必填，并转换成整数
               'mail'=>'required|end:@qq.com', // 必填，必须以@qq.com结尾
          ]
     ],
     // 这个规则是[单对象]，所以下面的规则会验证对象
     'info' => [
          'origin'=>'required|url', // 必填，需要为url格式
          'ip'=>'required|ip', // 必填，需要为ip格式
     ]
];
$params = $request->all();
@['err'=>$err,'msg'=>$msg] = $data = useValidate($params,$rules,$filter=true);
if($err === 400){
     return json(['err'=>400,'msg'=>$msg]);
}
```

### 流式处理

[](#流式处理)

> 之前想对一个字段进行复杂操作，需要写第一步，第二步，第三步...,现在直接可以写成一维数组形式，规则：`['int:100'] 数组只有一个规则代表：数组按照改规则处理`，当规则`不止一个`按照自动展开规则，再按照从左到右依次验证处理。

- 数组需要 `1~6个标签`
- 每个标签需要 `至少2-20个字符`
- 每个标签 是否包含违规字符
- 最终使用`,`符号连接成字符串，正式项目表单会比这还要复杂，现在可以像以下这么写。

```
$rule_one = ['label'=>'required|count:1,6']; // 验证整个 label字段 满足required|count:1,6即可
$rule_two = ['label'=>['length:2,20']]; // 循环验证 label字段 每个字段都验证 length:2,20
// 注意：数组规则(多个 大于1)时候： 字符串规则 数组符号[]包裹规则
// 如果数组规则只有一个 那个它表示 对数组进行 循环验证 这是 V2 版本的新特性
@['err'=>$err,'msg'=>$msg] = $data_one = useValidate($params,[
     'label'=>[
          'required|count:1,6', // 先验证 label 必填 在验证 数组长度 1-6 之间
          ['length:2,20'], // 验证 label数组每个元素的内容 长度 2-20 之间
          [function($field,$value){ // 验证 label数组每个元素的 内容
               if(useDetection($value)){
                    return $value;
               }
               throw new Exception("包含违禁词~");
          }],
          'join:,' // 对 label数组元素 使用 , 号连接
     ]
]);
if($err === 400){ return json(['err'=>400,'msg'=>$msg]); }
// 验证通过输出 是一个逗号连起来的长字符串
```

---

#### 规则的配置是和输入表单 是对应的关系，验证器会`预处理数据`并交给`后续过滤规则`，所以碰到需要处理`同一个键`同时进行`修改+验证的情况`,进行多次拆分即可。

[](#规则的配置是和输入表单-是对应的关系验证器会预处理数据并交给后续过滤规则所以碰到需要处理同一个键同时进行修改验证的情况进行多次拆分即可)

---

[![wallet.micateam](https://camo.githubusercontent.com/38ba1654dc36a80be838c1c39b719e1f7a94f64a30386ef2801261eb287abb17/68747470733a2f2f696d672e796f756c6f67652e636f6d2f77616c6c65742f6d6963617465616d2130)](https://camo.githubusercontent.com/38ba1654dc36a80be838c1c39b719e1f7a94f64a30386ef2801261eb287abb17/68747470733a2f2f696d672e796f756c6f67652e636f6d2f77616c6c65742f6d6963617465616d2130)

###  Health Score

43

—

FairBetter than 90% of packages

Maintenance95

Actively maintained with recent releases

Popularity15

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~26 days

Recently: every ~36 days

Total

15

Last Release

53d ago

Major Versions

v1.4.3 → v2.0.12026-03-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/bc8d0bccdb64e5c5462b792ebfaa8567b8dc2f7c40f8606eff892e367d285254?d=identicon)[youfeed](/maintainers/youfeed)

---

Top Contributors

[![youfeed](https://avatars.githubusercontent.com/u/22223510?v=4)](https://github.com/youfeed "youfeed (20 commits)")

### Embed Badge

![Health badge](/badges/youloge-webmanvalidate/health.svg)

```
[![Health](https://phpackages.com/badges/youloge-webmanvalidate/health.svg)](https://phpackages.com/packages/youloge-webmanvalidate)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
