axm/validation
最新稳定版本:1.0.9
Composer 安装命令:
composer require axm/validation
包简介
Simple, validation of rules
README 文档
README
Certainly! Here's comprehensive documentation for the Validator class along with examples of how to use it. This documentation is formatted for GitHub:
Validator Class
The Validator class is a versatile tool for data validation in PHP applications. It allows you to define validation rules and check if data meets those rules. This documentation provides an in-depth guide to using the Validator class.
Table of Contents
- Class Overview
- Installation
- Basic Usage
- Validation Rules
- Advanced Usage
- Examples
- Contributing
- License
1. Class Overview
The Validator class provides the following features:
- Validation of data against user-defined rules.
- Support for custom validation rules.
- Rule chaining for complex validation scenarios.
- Detailed error reporting with customizable error messages.
2. Installation
To use the Validator class, follow these steps:
composer require axm/validation
3. Basic Usage
Here's a simple example of how to use the Validator class:
use Axm\Validation\Validator; $rules = [ 'username' => 'required|string|min:5|max:20', 'email' => 'required|email', ]; $data = [ 'username' => 'john_doe', 'email' => 'johndoe@example.com', ]; $validator = Validator::make($rules, $data); if ($validator->validate()) { echo "Data is valid!"; } else { $errors = $validator->getErrors(); print_r($errors); }
4. Validation Rules
The Validator class supports a variety of validation rules, including required, string, email, min, max, and custom rules. You can chain rules together using the | separator.
Available Rules
required: Ensures the field is present and not empty.string: Checks if the field is a string.email: Validates the field as an email address.min:value: Checks if the field's length or value is greater than or equal tovalue.max:value: Checks if the field's length or value is less than or equal tovalue.- Custom Rules: You can define your own custom validation rules.
5. Advanced Usage
Custom Validation Rules
You can create custom validation rules by defining a class that implements the Axm\Validation\Rules\RuleInterface. Here's an example:
use Axm\Validation\Rules\RuleInterface; class CustomRule implements RuleInterface { public function validate($value, array $parameters = []): bool { // Implement your custom validation logic here. return /* validation result */; } public function message(): string { // Define a custom error message for this rule. return 'Custom validation failed.'; } }
Adding Custom Rules
To add custom rules to the Validator, use the addRules method:
$customRules = [ 'custom_rule' => CustomRule::class, ]; $validator->addRules($customRules);
6. Examples
Here are more examples of how to use the Validator class:
Example 1: Conditional Validation
$rules = [ 'password' => 'required|string|min:8', 'confirm_password' => 'required|string|same:password', ]; $data = [ 'password' => 'mysecurepassword', 'confirm_password' => 'mysecurepassword', ]; $validator = Validator::make($rules, $data);
Example 2: Unique Rule
$rules = [ 'email' => 'required|string|unique:users,email', ]; $data = [ 'email' => 'johndoe@example.com', ]; $validator = Validator::make($rules, $data);
7. Contributing
If you want to contribute to the Validator class, please follow the contributing guidelines.
8. License
The Validator class is open-source software licensed under the MIT License.
This documentation should help you get started with the Validator class and demonstrate various use cases. For more details and advanced usage, refer to the class source code and examples provided in the GitHub repository.
Feel free to adapt and expand upon this documentation as needed for your project.
axm/validation 适用场景与选型建议
axm/validation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 126 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「microframework」 「php」 「rest」 「router」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 axm/validation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 axm/validation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 axm/validation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A lightweight, secure-by-default PHP microframework built on Slim – providing Laravel-like features (ORM, authentication, migrations, caching) without the bloat. Perfect for building REST APIs and small-to-medium PHP applications.
Alfabank REST API integration
Reducido núcleo de trabajo para PHP
A Slim Framework MVC skeleton application for rapid development
A TidyPHP is a micro Framework for build fast and tidy web applications
统计信息
- 总下载量: 126
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-28