mousav1/validify
最新稳定版本:v2.0.2
Composer 安装命令:
composer require mousav1/validify
包简介
Validify is a powerful PHP validation library that provides flexible and customizable validation rules, error handling, and pre-validation callbacks.
README 文档
README
Table of Contents
- Validify
Introduction
Validify is a lightweight and extensible PHP validation library that simplifies validating user inputs. Whether you're building an API, a form, or any application requiring data validation, Validify provides a powerful and intuitive way to handle it.
Features
- Simple API: Easy-to-use API for defining and executing validation rules.
- Extensible: Define custom validation rules to fit specific use cases.
- Pre-Validation Callbacks: Execute custom logic before validation.
- Conditional Validation: Dynamically apply rules based on conditions.
- Nested Data Support: Validate complex data structures using dot notation.
- Comprehensive Built-in Rules: Includes a wide range of predefined validation rules.
Installation
To install the package, use Composer:
composer require mousav1/validify
Quick Start
Basic Validation
Here is an example of simple validation using Validify:
use Mousav1\Validify\Validator; $data = [ 'username' => 'john_doe', 'email' => 'john@example.com', 'age' => 25, ]; $validator = new Validator($data, [ 'username' => ['required', 'alpha'], 'email' => ['required', 'email'], 'age' => ['required', 'numeric', 'min:18'], ]); if (!$validator->validate()) { print_r($validator->getErrors()); }
Fluent Validation
Define validation rules using a fluent interface for better readability:
use Mousav1\Validify\Validator; $data = [ 'name' => 'prefix_name', ]; $validator = new Validator($data); $validator->field('name') ->required() ->applyRules(); if (!$validator->validate()) { print_r($validator->getErrors()); }
Advanced Usage
Custom Validation Rules
Extend Validify with your own custom rules:
use Mousav1\Validify\Validator; Validator::extend('even', function () { return new class extends \Mousav1\Validify\Rules\Rule { public function passes($field, $value, array $data): bool { return $value % 2 === 0; } public function name(): string { return 'even'; } public function message($field): string { return "{$field} must be an even number."; } }; }); $data = ['number' => 3]; $validator = new Validator($data, [ 'number' => ['even'] ]); if (!$validator->validate()) { print_r($validator->getErrors()); }
Conditional Validation
Apply rules dynamically based on conditions:
$data = [ 'age' => 20, 'license' => '' ]; $validator = new Validator($data); $validator->addConditionalRule('license', ['required'], function ($data) { return $data['age'] > 18; }); if (!$validator->validate()) { print_r($validator->getErrors()); }
Date and Time Validation
Validate date and time fields using predefined rules:
Date Format
$data = [ 'birthdate' => '2024-09-01', ]; $validator = new Validator($data, [ 'birthdate' => ['required', 'date_format:Y-m-d'], ]); if (!$validator->validate()) { print_r($validator->getErrors()); }
After and Before Rules
$data = [ 'start_date' => '2024-01-01', 'end_date' => '2024-02-01', ]; $validator = new Validator($data, [ 'end_date' => ['required', 'date_format:Y-m-d', 'after:start_date'], ]); if (!$validator->validate()) { print_r($validator->getErrors()); }
Custom Error Messages
Define custom error messages for specific rules:
use Mousav1\Validify\Validator; $data = [ 'username' => '', 'email' => 'invalid-email', ]; $validator = new Validator($data, [ 'username' => ['required'], 'email' => ['required', 'email'], ]); $validator->setCustomMessages([ 'username.required' => 'The username field cannot be empty.', 'email.email' => 'Please provide a valid email address.', ]); if (!$validator->validate()) { print_r($validator->getErrors()); }
Field Aliases
Use aliases for more readable error messages:
$validator->setAliases([ 'email' => 'Email Address' ]); $validator->validate(); print_r($validator->getErrors());
Pre-Validation Callbacks
Execute logic before validation starts:
$validator->beforeValidate(function (&$data) { $data['username'] = strtolower($data['username']); });
Available Rules
- required
- min
- max
- numeric
- confirmed
- url
- in
- between
- regex
- alpha
- optional
- required_with
- array
- integer
- boolean
- not_in
- uppercase
- lowercase
- json
- date_format
- after
- before
Contributing
Contributions are welcome! Feel free to submit a pull request or open an issue.
Steps to Contribute:
- Fork the repository.
- Create a new branch for your feature/bugfix.
- Write clear and descriptive commit messages.
- Submit a pull request with detailed explanation.
License
This package is licensed under the MIT License. See the LICENSE file for more details.
mousav1/validify 适用场景与选型建议
mousav1/validify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「validation」 「rules」 「error-handling」 「custom-validation」 「data-validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mousav1/validify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mousav1/validify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mousav1/validify 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Magento 2 module which adds support for Speculation Rules API for instant page loads.
The coding standard applying to all Youdot PHP projects, based on Doctrine set of PHPCS rules, with additional checks.
Business rules engine tools.
A set of Laravel validation rules
Adds request-parameter validation to the SLIM 3.x PHP framework
The last validation library you will ever need!
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-27