定制 armin-dev/validx 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

armin-dev/validx

Composer 安装命令:

composer require armin-dev/validx

包简介

A modern PHP validation library.

README 文档

README

A lightweight and extensible PHP validation library.
Validx provides a simple and readable way to validate data in PHP projects.

Features (v1)

  • Basic validation rules: required, email, min, max, between, numeric, integer, phone, url, secure, unique, in, alphanumeric, date, fileType, maxfileSize.
  • Supports arrays and nested fields.
  • Optional PDO database injection for rules like unique.
  • Integration-ready for forms, APIs, and any PHP project.

Quick Start

use Validx\Validation;

$validation = new Validation();

$data = [
    'username' => 'exampleuser',
    'email' => 'example@example.com',
];

$rules = [
    'username' => 'required | between:2,15',
    'email' => 'required | email',
];

$errors = $validation->validate($data, $rules);

if (!empty($errors)) {
    print_r($errors);
} else {
    echo "All fields are valid!";
}

## Example Error Output

```php
[
    'username' => ['The username must have between 2 and 15 characters'],
    'email' => ['The email already exists'],
]

Available Rules (v1)

  • required
  • email
  • min
  • max
  • between
  • numeric
  • integer
  • phone
  • url
  • secure
  • unique
  • in
  • alphanumeric
  • date
  • fileType
  • maxfileSize

Writing Validation Rules

Note: All keys defined in your validation rules must exist in the input data array.

Validation rules are defined as strings using pipe | separators. You can combine multiple rules per field.

Examples:

$rules = [
    'username' => 'required | between:3,20',
    'email' => 'required | email | unique:email',
    'password' => 'required | min:6 | max:12 | secure',
    'age' => 'integer | min:18',
];

required — The field must not be empty.

email — Must be a valid email address.

unique:email — Must be unique in the database (requires PDO).

min:value / max:value — Minimum or maximum length/value.

between:min,max — Value must be within the given range.

integer — Must be an integer.

secure — Must meet password security criteria

Error Messages

Validx returns error messages from an internal enum class.
Currently, messages are not customizable, but each rule has a predefined message.

Available Messages Per Rule

Rule Default Message
REQUIRED Please Enter The %s
EMAIL The %s is not a valid email address
MIN The %s must have at least %s characters
MAX The %s must have at most %s characters
BETWEEN The %s must have between %d and %d characters
SAME The %s must match with %s
ALPHANUMERIC The %s should have only letters and numbers
SECURE The %s must have between 8 and 64 characters and contain at least one number, one upper case letter, one lower case letter and one special character
UNIQUE The %s already exists
NUMERIC The %s must contain only numbers
INTEGER The %s must be an integer
URL The %s is not a valid URL
DATE The %s is not a valid date
PHONE The %s is not a valid phone number
IN The %s must be one of the allowed values
FILETYPE The %s must be a valid file type: %s
MAXFILESIZE The %s must not be larger than %s

Note: %s and %d are placeholders that will be replaced with field names, min/max values, or file size as appropriate.

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-24