PHPackages                             baagee/php-params-validator - 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. baagee/php-params-validator

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

baagee/php-params-validator
===========================

PHP Params Validator Library

v0.0.8(6y ago)1541MITPHPPHP &gt;=7.0

Since Mar 26Pushed 6y agoCompare

[ Source](https://github.com/baagee/php-params-validator)[ Packagist](https://packagist.org/packages/baagee/php-params-validator)[ RSS](/packages/baagee-php-params-validator/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (8)DependenciesVersions (9)Used By (1)

php-params-validator
====================

[](#php-params-validator)

[![Build Status](https://camo.githubusercontent.com/19b76ebc2cdd78ff2483af8951f825b8897fa540df3b85e0b9c5181f7b916040/68747470733a2f2f7777772e7472617669732d63692e6f72672f6261616765652f7068702d706172616d732d76616c696461746f722e7376673f6272616e63683d6d6173746572)](https://www.travis-ci.org/baagee/php-params-validator)

PHP Params Validator Library

### PHP参数验证类

[](#php参数验证类)

### 内置以下验证规则

[](#内置以下验证规则)

```
AlphaDashRule.php
AlphaNumRule.php
AlphaRule.php
BankIdRule.php
ChineseRule.php
DateRule.php
EmailRule.php
EnumRule.php
EqualRule.php
FloatRule.php
IdCardRule.php
IntegerRule.php
IpRule.php
JsonRule.php
MacRule.php
NumberRule.php
PhoneRule.php
PlateNoRule.php
QqRule.php
RegexpRule.php
StringRule.php
UrlRule.php
ZipRule.php

```

### 安装使用

[](#安装使用)

composer require baagee/php-params-validator

### 使用示例

[](#使用示例)

#### 验证一个字段

[](#验证一个字段)

```
include_once __DIR__ . '/../vendor/autoload.php';

// 单例模式 获取validator对象
$validator  = \BaAGee\ParamsValidator\Validator::getInstance();

try {
    $res = $validator->addRules('order_id', '1234567890', [
        ['number', '订单号不是数字'],
        ['number|min[10]|max[10]', '订单号不是10位数字'],
    ])->validate();
    var_dump($res);
} catch (\BaAGee\ParamsValidator\Base\ParamInvalid $e) {
    die('Error:' . $e);
}
```

#### 批量验证

[](#批量验证)

```
//要验证的数据
$data = [
    'name'        => 'lotly',
    'age'         => '',
    'sex'         => 1,
    'phone'       => 17878787870,
    'birthday'    => '2019-09-23',
    'weight'      => 50.4,
    'password'    => 'password098',
    'regexp'      => 'dsfa78sdgs-',
    'macAddress'  => '00-01-6C-06-A6-29',
    'IdCard'      => '41282319900909121X',
    'email'       => '32r2345234@qq.x',
    'zip'         => 240990,
    'homePage'    => 'http://sdfgs.com/asdgfsd?fds=43t&sfds=90',
    'ip'          => '234.32.32.90',
    'ext'         => '{"money":342.3}',
    'bankId'      => '621081257000009900',
    'telephone'   => '010-86551122',
    'chinese'     => '溜溜溜溜',
    'alphaDash'   => 'er2454_34t4-knj',
    'plateNo'     => '京Q9087P',
    'alphaNumber' => 'sdgs34t35ad',
    'alpha'       => 'asdgsege',
    'number'      => '234543456',
    'qq'          => '90876543212',
    'optional'    => '87uhu哈',
    'service'=>'400-021-9999'
];
// 数据对应的验证规则
$rules = [
    // 一个参数支持多个验证规则 二维数组
    'age'         => [
        ['integer|optional|default[20]', '年龄不合法1'],
        ['integer|required|min[18]|max[24]', '年龄不合法2']
    ],
    'qq'          => [
        ['number|required', 'QQ不是纯数字'],
        ['qq', '不是QQ号码']
    ],
    'name'        => ['string|default[eee]|min[2]|max[40]', '名字不合法'],
    'weight'      => ['float|min[40.0]|max[55.0]', '体重不合法'],
    'birthday'    => ['date|format[Y-m-d]', '出生日期不合法'],
    'sex'         => ['enum|allows[1,2]', '性别不合法'],
    'password'    => ['equal|this[password098]', '密码不正确'],
    'regexp'      => ['regexp|pattern[/[a-z0-9]+/]', 'regexp不合法'],
    'phone'       => ['phone|type[mobile]', '手机号不合法'],
    'macAddress'  => ['mac', 'mac地址不合法'],
    'optional'    => ['string|optional|min[6]', 'optional最小6位'],
    'IdCard'      => [
        ['alphaNum|min[18]|max[18]', '身份证不是16位'],
        ['IdCard', '身份证不合法'],
    ],
    'email'       => ['email', '邮箱不合法'],
    'zip'         => ['zip', '邮政编码不合法'],
    'homePage'    => ['url', '个人主页不合法'],
    'ip'          => ['ip|start[10.188.0.25]|end[10.188.40.25]', 'IP不合法'],
    'ext'         => ['json|decode', 'Ext Json不合法'],
    'bankId'      => ['BankId', '银行卡号不合法'],
    'telephone'   => ['phone|type[land]', '座机号码不合法'],
    'chinese'     => ['chinese|min[2]|max[4]', '不是纯中文或者长度不在2-4范围内'],
    'alphaDash'   => ['alphaDash', '不是字母数字—_-'],
    'plateNo'     => ['plateNo', '车牌号不合法'],
    'alphaNumber' => ['alphaNum', '不是字母数字组合'],
    'alpha'       => ['alpha', '不是纯字母'],
    'number'      => ['number', '不是纯数字'],
    'service'     => ['phone|type[service]', '不是服务热线'],
];
try {
    $data = $validator->batchAddRules($data, $rules)->validate();
    var_dump($data);
} catch (\BaAGee\ParamsValidator\Base\ParamInvalid $e) {
    die('Error:' . $e);
}
```

#### 单个验证规则

[](#单个验证规则)

```
// 使用单个验证规则
$stringRule = new \BaAGee\ParamsValidator\Rules\StringRule();
// 验证一个字段是不是6-12位字符串
$res        = $stringRule->setParams(['min' => 6, 'max' => 12])->check('123jk45');
var_dump($res);
if ($res === false) {
    echo '验证失败' . PHP_EOL;
}
// 或者
$res = (new \BaAGee\ParamsValidator\Rules\StringRule(['min' => 6, 'max' => 12]))->check('123jk45');
var_dump($res);
if ($res === false) {
    echo '验证失败' . PHP_EOL;
}
```

#### 自定义验证规则

[](#自定义验证规则)

假设验证一个数字是不是5的倍数

```
include_once __DIR__ . '/../vendor/autoload.php';

/**
 * Class MyRule 自定义验证规则 验证是否为某个数的倍数
 * 继承\BaAGee\ParamsValidator\Base\RuleAbstract
 */
class MyRule extends \BaAGee\ParamsValidator\Base\RuleAbstract
{
    /**
        实现具体的check方法
     * @param $value
     * @return array|bool|mixed
     */
    public function check($value)
    {
        $value = intval($value);
        if ($value % $this->params['mo'] !== 0) {
            return false;
        }
        return ['data' => $value];
    }
}

$validator = \BaAGee\ParamsValidator\Validator::getInstance();

try {
    // 注意自定义规则处理类的命名空间，要传完全限定名字
    $validator->addMyRule('num', 29, sprintf('%s|mo[5]', MyRule::class), 'num不是5的倍数');
    $res = $validator->validate();
    var_dump($res);
} catch (\BaAGee\ParamsValidator\Base\ParamInvalid $exception) {
    // 获取出错的参数信息 验证规则，参数名 ，参数值
    echo sprintf('rule: %s ; field: %s ; value: %s ;' . PHP_EOL, $exception->getRule(), $exception->getField(), $exception->getValue());
    die('Error:' . $exception);
}
```

### 其他详细使用见tests目录

[](#其他详细使用见tests目录)

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity51

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 ~30 days

Recently: every ~49 days

Total

8

Last Release

2444d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/22995500?v=4)[BaAGee](/maintainers/baagee)[@baagee](https://github.com/baagee)

---

Top Contributors

[![baagee](https://avatars.githubusercontent.com/u/22995500?v=4)](https://github.com/baagee "baagee (1 commits)")

---

Tags

phpvalidatorvalidateparams

### Embed Badge

![Health badge](/badges/baagee-php-params-validator/health.svg)

```
[![Health](https://phpackages.com/badges/baagee-php-params-validator/health.svg)](https://phpackages.com/packages/baagee-php-params-validator)
```

PHPackages © 2026

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