Download the PHP package migears/validator without Composer

On this page you can find all versions of the php package migears/validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package validator

migears/validator

Version

Lightweight, declarative validation library for PHP. Error-code based, i18n-ready — no hardcoded messages.

Validator provides a clean API for validating arrays (form data, API parameters, domain objects) with a simple rule syntax. Validation errors are returned as structured error codes + parameters, ready for translation via any i18n library.

Background: miGears is the open-source successor of TinyGears, a self-developed PHP framework. It was renamed and open-sourced recently because the name TinyGears is already taken in the open-source community.

Features

Boundaries

In scope

Not in scope (by design)

Installation

Requires: PHP 8.1+, ext-mbstring.

Quick Start

Error format

Errors are returned as structured data — error code + params, not hardcoded messages. This makes translation and programmatic handling easy.

💡 validate() returns an error array, not a boolean. An empty array means all rules passed. Use $validator->passes($data, $rules) when you just need a boolean result — if ($v->validate(...)) is always truthy when there is at least one error, which is the opposite of what the name suggests.

Handling Errors

Validator produces data — you decide how to present it. Below are three common patterns.

1. Server-side rendering (with migears/i18n)

Translate errors to user-friendly messages in your controller/handler:

2. API / Frontend-backend separation

Return errors as-is — let the frontend handle translation:

Frontend receives structured errors and translates them with its own i18n library:

3. Programmatic handling (CLI / services)

Use error codes for logic decisions:

With Domain Objects

Use migears/domain's Validatable trait for self-validating domain objects:

Rule Configuration

Boolean — enable with defaults

⚠️ true always means "enable with default config", never "set the parameter to true". Three consequences:

  • To pass true to a boolean parameter use the array form: ['containUrl' => ['invert' => true]].
  • Some rules run with a useless default when keyed true. ['pattern' => true] uses the empty pattern and matches everything. ['equals' => true] compares against null, so it almost always fails for any non-empty value. ['enum' => true] enables enum with an empty allowed set, so it rejects every non-empty value. To actually enforce a pattern/equality/set, pass an explicit scalar or list.
  • Only strict false disables a rule: ['required' => false] skips the rule. Loose falsy values like 0, '', or null do not disable; they are treated as scalar config and may enable the rule.

Scalar — set the main parameter

Scalar values are coerced to the rule's parameter type, so string numerics also work: ['minLength' => '5'], ['containUrl' => 1]. Boolean parameters also accept the literals '1'/'true' and '0'/'false'.

Array — named form (constructor parameter names)

Keys are the rule's constructor parameter names. An unknown key raises InvalidArgumentException listing the valid keys, so a typo fails loudly instead of silently turning the rule into a no-op.

Array — list form (positional values)

When the rule's first parameter accepts an array (e.g. enum), the whole list becomes that argument. Otherwise a one-element list is treated as the scalar config (coercion applies), and a longer list raises InvalidArgumentException.

A config with nowhere to go is refused rather than dropped: a truthy scalar or one-element list handed to a rule whose constructor takes no argument raises InvalidArgumentException, the same loud failure an unknown key gets. Loose falsy values (0, '') remain the way to write "enabled, no config".

Instance — pass a rule directly

Zero-index alias — enable a rule by name only

Rules in this array form take a string alias as the value and apply the rule with its default configuration. A field's rules value must be an array of rules; a non-array value such as ['name' => 'required'] raises InvalidArgumentException instead of validating nothing.

Empty-value semantics

Every built-in rule (except required) treats a null or blank-string value as valid — i.e. it is skipped. Only required can force a field to be present. For example ['email' => true] passes when email is absent; add 'required' => true to make it mandatory. An empty array is only "empty" to required; every other rule sees [] as an ordinary value and rejects it.

Built-in Rules

Rule Params Description
required [] Value cannot be null, empty string, or empty array
email [] Valid email address
integer [] Integer or integer string
number [] Numeric value (int, float, or numeric string)
min {min} Numeric value ≥ min
max {max} Numeric value ≤ max
minLength {min} Minimum string length (multibyte-safe)
maxLength {max} Maximum string length (multibyte-safe)
pattern {pattern} Regex pattern match
url [] Valid URL
date [] Real, valid calendar date in YYYY-M-D format
time [] Real time in H:M(:S) format (0–23, 0–59)
money [] Money amount: zero or positive decimal with ≤ 2 places
enum {allowed} Value is one of the allowed values (array or a\|b string)
ipAddress [] Valid IPv4 or IPv6 address
alpha [] Alphabetic characters only (a-zA-Z)
alphaNumeric [] Alphanumeric characters only (a-zA-Z0-9)
equals {expected} Strictly equals the expected value
greaterThan {threshold} Numeric value > threshold
greaterOrEqualThan {threshold} Numeric value ≥ threshold
lessThan {threshold} Numeric value < threshold
lessOrEqualThan {threshold} Numeric value ≤ threshold
containUrl {invert} Contains a URL (inverted when invert is true)

The rule name and the error code are the same; the Params column shows the entries returned in the error, i.e. the interpolated variables for i18n messages. alpha and alphaNumeric are ASCII-only — they do not accept accented or CJK (e.g. Chinese) characters.

The numeric rules (number, min, max, greaterThan, greaterOrEqualThan, lessThan, lessOrEqualThan) rely on PHP's is_numeric(), so a numeric string padded with surrounding spaces (' 123', '123 ') is accepted. The format rules that use anchored regexes (integer, money, alpha, alphaNumeric, date, time) reject it.

Custom Rules

Implement RuleInterface:

Register and use — custom rules are scoped to each Validator instance:

The rule alias is derived from the class short name: StrongPasswordRule → strongPassword. Because this form builds the rule from the config in the rules array, a rule's parameters live there, not at the registration:

To replace a built-in rule, name your class to collide with it (e.g. EmailRule in your own namespace overrides email). Because registration is per instance, custom rules never leak into other validation contexts.

A rule may also be registered as an instance — the form for one that needs a dependency (a DAO lookup, say). Its alias is its own getErrorCode(), so it need not be a named class: anonymous classes and mocks work too. It is already built, so any config given for it in the rules array is ignored — do not use this form to pass parameters.

You can also pre-register rules in the constructor for a ready-to-use instance:

API Reference

Method Description
new Validator(array $ruleClasses = []) Create a validator instance, optionally pre-registering custom rule classes in one shot
validate(array $data, array $rules): array Validate data, return errors
passes(array $data, array $rules): bool Check if validation passes
$validator->register(class-string\|RuleInterface $rule): bool Register a custom rule on this instance — a class-string (alias from the class name) or an instance (alias from its getErrorCode()); returns true if it overrode an existing rule

Design Philosophy

miGears Validator follows the miGears philosophy: minimal, readable, and useful.

License

MIT


migears/validator

Version

轻量级声明式 PHP 验证库。基于错误码,i18n 友好 —— 没有硬编码的消息。

Validator 提供简洁的 API 来验证数组(表单数据、API 参数、领域对象),使用简单的规则语法。验证错误以结构化的错误码 + 参数形式返回,可直接通过任何 i18n 库进行翻译。

特性

边界

范围内

范围外(刻意不做)

安装

要求:PHP 8.1+,ext-mbstring。

快速开始

错误格式

错误以结构化数据返回 —— 错误码 + 参数,而非硬编码消息。这样翻译和程序化处理都很方便。

💡 validate() 返回的是错误数组,不是布尔值。空数组表示全部通过。只需布尔结果时请用 $validator->passes($data, $rules) —— 注意 if ($v->validate(...)) 在有错误时恒为真,语义与字面直觉相反。

错误处理

Validator 只产出数据 —— 如何展示由你决定。以下是三种常见模式。

1. 服务端渲染(配合 migears/i18n)

在控制器/处理器中将错误翻译为用户友好的消息:

2. API / 前后端分离

原样返回错误 —— 让前端处理翻译:

前端收到结构化错误后,用自己的 i18n 库翻译:

3. 程序化处理(CLI / 服务层)

用错误码做逻辑判断:

与领域对象配合

使用 migears/domain 的 Validatable trait 实现自验证领域对象:

规则配置

布尔值 — 启用默认配置

⚠️ true 始终表示"以默认配置启用",绝不表示"把参数设为 true"。三个后果:

  • 要传 true 给布尔参数请用数组形式:['containUrl' => ['invert' => true]]。
  • 某些规则配 true 时会使用无意义的默认值:['pattern' => true] 使用空正则,什么都匹配;['equals' => true] 与 null 比较,对任意非空值几乎必然失败;['enum' => true] 以空允许集启用,拒绝一切非空值。要真正校验格式/相等/枚举集合,请传明确的标量或列表。
  • 只有严格 false 才禁用规则:['required' => false] 会跳过该规则。0、''、null 等松散假值不会禁用,它们被当作标量配置处理,可能反而启用规则。

标量值 — 设置主要参数

标量值会按规则参数类型自动适配,因此数字字符串也可用:['minLength' => '5']、['containUrl' => 1]。布尔参数还接受字面量 '1'/'true' 与 '0'/'false'。

数组 — 命名形态(构造参数名作键)

键为规则的构造参数名。未知键会抛 InvalidArgumentException 并列出合法键,因此拼写错误会立刻报错,而不是把规则静默变成空操作。

数组 — 列表形态(位置取值)

当规则的首参接受数组(如 enum)时,整个列表作为该参数传入;否则单元素列表按标量配置处理(含类型适配),多元素列表则抛 InvalidArgumentException。

无处可去的配置会被拒绝,而不是被丢弃:把真值标量或单元素列表交给一个构造器不接受任何参数的规则,会抛 InvalidArgumentException,与未知键得到的是同一种响亮失败。假值(0、'')仍照旧写作「启用、不带配置」。

实例 — 直接传入规则

零索引别名 — 仅按名称启用规则

此形态的值为字符串规则名,使用默认配置启用该规则。字段的规则取值必须是规则数组;像 ['name' => 'required'] 这样的非数组取值会抛 InvalidArgumentException,而不是什么都不校验。

空值语义

内置所有规则(required 除外)都把 null 或空白字符串视为合法——即自动跳过。只有 required 能强制字段必填。例如 ['email' => true] 在缺少 email 时通过;要强制必填需加上 'required' => true。空数组只对 required 算「空」;其它规则会把 [] 当作普通取值并判为失败。

内置规则

规则 参数 说明
required [] 值不能为 null、空字符串或空数组
email [] 有效的邮箱地址
integer [] 整数或整数字符串
number [] 数值(int、float 或数字字符串)
min {min} 数值 ≥ min
max {max} 数值 ≤ max
minLength {min} 最小字符串长度(多字节安全)
maxLength {max} 最大字符串长度(多字节安全)
pattern {pattern} 正则表达式匹配
url [] 有效的 URL
date [] 真实合法的日历日期,YYYY-M-D 格式
time [] 真实合法的时间,H:M(:S) 格式(0–23、0–59)
money [] 金额:0 或最多两位小数的正数
enum {allowed} 值在允许集合内(数组或 a\|b 字符串)
ipAddress [] 有效的 IPv4 或 IPv6 地址
alpha [] 仅英文字母(a-zA-Z)
alphaNumeric [] 仅字母数字(a-zA-Z0-9)
equals {expected} 与期望值严格相等
greaterThan {threshold} 数值 > threshold
greaterOrEqualThan {threshold} 数值 ≥ threshold
lessThan {threshold} 数值 < threshold
lessOrEqualThan {threshold} 数值 ≤ threshold
containUrl {invert} 包含 URL(invert 为 true 时取反)

规则名即错误码;「参数」列是出错时返回的字段,即 i18n 消息用于插值的变量。alpha 与 alphaNumeric 仅支持 ASCII,不接受带重音或 CJK(如中文)字符。

数值类规则(number、min、max、greaterThan、greaterOrEqualThan、lessThan、lessOrEqualThan)基于 PHP 的 is_numeric(),因此前后带空格的数字串(' 123'、'123 ')会被接受;而使用锚定正则的格式类规则(integer、money、alpha、alphaNumeric、date、time)会拒绝。

自定义规则

实现 RuleInterface:

注册并使用——自定义规则只会作用在当前 Validator 实例上:

规则别名由类短名推导:StrongPasswordRule → strongPassword。这种形态会依据规则数组里的配置来构造规则,因此规则的参数写在规则数组里,而不是写在注册处:

若要覆盖内置规则,把外部类命名成与之重名即可(例如自己命名一个 EmailRule 就能覆盖内置的 email)。因为注册是基于实例的,自定义规则不会泄漏到其它验证场景。

规则也可以按实例注册 —— 适用于需要依赖(比如查库)的规则。它的别名取自自身的 getErrorCode(),因此不必是具名类:匿名类与 mock 同样可用。它已经构造完成,所以规则数组里为它写的配置会被忽略 —— 不要用这种形态传参。

也可以在构造器里一次性预注册,得到一个开箱即用的实例:

API 参考

方法 说明
new Validator(array $ruleClasses = []) 创建验证器实例,可选地在构造时一次性预注册自定义规则类
validate(array $data, array $rules): array 验证数据,返回错误
passes(array $data, array $rules): bool 检查验证是否通过
$validator->register(class-string\|RuleInterface $rule): bool 在当前实例注册自定义规则 —— 传类名(别名由类名推导)或传实例(别名取自其 getErrorCode());返回 true 表示覆盖了已有规则

设计哲学

miGears Validator 遵循 miGears 设计哲学:极简、可读、实用。

许可证

MIT


All versions of validator with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
ext-mbstring Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package migears/validator contains the following files

Loading the files please wait ...