PHPackages                             sing/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. sing/validate

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

sing/validate
=============

Validate组件提供了方便的验证机制，快速实现验证业务。

1.0.16(8y ago)02831MITPHPPHP &gt;=5.4.0

Since May 22Pushed 8y ago1 watchersCompare

[ Source](https://github.com/gitzhuhe/validate)[ Packagist](https://packagist.org/packages/sing/validate)[ RSS](/packages/sing-validate/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

修改于
===================================

[](#修改于-httpsgithubcomhoudunwang)

自动验证
====

[](#自动验证)

Validate组件提供了方便的验证机制，快速实现验证业务。 \[TOC\]

安装
--

[](#安装)

使用 composer 命令进行安装或下载源代码使用。

```
composer require sing/validate

```

使用
--

[](#使用)

#### 系统规则

[](#系统规则)

```
required                 	必须输入
email                    	邮箱
http                        网址
tel                         固定电话
phone                    	手机
zipCode                     邮政编码
num                      	数字范围 如：num:20,60
range                       长度范围(位数)如 :range:5,20
maxlen                   	最大长度如：maxlen:10
minlen                   	最小长度如：minlen:10
regexp                      正则如：regexp:/^\d{5,20}$/
confirm                  	两个字段值比对如：confirm:password2
china                   	验证中文
identity                	身份证
unique					   数据表值唯一如:unique:news,id (id为表主键）(抛弃)
exists					    存在字段时验证失败
captcha					 验证码(抛弃)

```

#### 基本语法

[](#基本语法)

```
array(字段名,验证方法,错误信息,验证条件)
验证条件 (可选)：
	1	有字段时
	2	值不为空时
	3	必须处理 (默认)
	4	值为空时
	5   不存在字段时处理

```

#### 闭包验证

[](#闭包验证)

```
\sing\validate\Validate::make(
    [
        ['domain', function ($value) {
            return $value > 100;
        }, '域名不能为空', 3 ]
	]);
//闭包返回 true 时验证通过

```

#### 验证表字段唯一性

[](#验证表字段唯一性)

```
\sing\validate\Validate::make( [
	[ 'qq', 'unique:user,uid', 'qq已经存在', 3 ]
	// user ：表名  uid：表主键
] );

```

#### 验证表单验证码

[](#验证表单验证码)

```
\sing\validate\Validate::make( [
 [ 'code', 'captcha', '验证码输入错误', 3 ]
 ] );

```

处理方式 （现在一律返回错误）
---------------

[](#处理方式-现在一律返回错误)

验证响应由配置文件 system/config/error.php 中的app 配置段决定。

#### redirect(抛弃)

[](#redirect抛弃)

直接跳转,会分配变量$errors到前台，开发者可以通过模板标签读取错误内容。

#### show（抛弃）

[](#show抛弃)

直接显示错误信息，不需要开发者处理

#### default （抛弃）

[](#default-抛弃)

由开发者自行处理，需要使用 \\sing\\validate\\Validate::fail()自行进行判断。

扩展使用
----

[](#扩展使用)

#### 增加规则

[](#增加规则)

```
\sing\validate\Validate::extend('checkUser',function($field,$value,$params){
	//返回值为true时验证通过
		return true;
});

```

第一个参数为验证规则名称，第二参数闭包函数。

#### 验证判断

[](#验证判断)

需要响应方式为default值时有效。

```
if(\sing\validate\Validate::fail()){
	echo '验证失败';
};

```

#### 链式操作

[](#链式操作)

```
\sing\validate\Validate::make(array(
        array('username','required|email','邮箱不能为空')
    ))->fail();

```

#### 获取错误信息

[](#获取错误信息)

错误信息会记录到模型对象的 error 属性中，使用 getError() 方法获取但需要设置处理方式为 default

```
\sing\validate\Validate::getError();

```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 74.1% 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 ~53 days

Total

3

Last Release

3173d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f311446c8f21e389454a3cb26f97fc63a13689987b58c38663a9d50b2b6a19d?d=identicon)[gitzhuhe](/maintainers/gitzhuhe)

---

Top Contributors

[![houdunwang](https://avatars.githubusercontent.com/u/13848593?v=4)](https://github.com/houdunwang "houdunwang (20 commits)")[![gitzhuhe](https://avatars.githubusercontent.com/u/16486483?v=4)](https://github.com/gitzhuhe "gitzhuhe (7 commits)")

---

Tags

Sing

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sing-validate/health.svg)

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

###  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)
