hsnfirdaus/class-validator
最新稳定版本:v2.0.4
Composer 安装命令:
composer require hsnfirdaus/class-validator
包简介
Class Validator using php attribute
README 文档
README
PHP Class Validator using php 8.0 attributes.
Caution
This package no longer maintained, use symfony/validator instead.
Installation
Using Composer:
composer require "hsnfirdaus/class-validator"
Usage
Example Usage:
<?php declare(strict_types=1); namespace MyApp\Contract\User; use Hsnfirdaus\ClassValidator\Attribute\IsEnum; use Hsnfirdaus\ClassValidator\Attribute\IsNotEmpty; use Hsnfirdaus\ClassValidator\Attribute\IsOptional; use Hsnfirdaus\ClassValidator\Validator; use MyApp\Entity\Enum\UserRole; use MyApp\Entity\Enum\UserType; class AddUserContract { #[Name(name: 'User Type')] #[IsNotEmpty] #[IsEnum(enum: UserType::class)] public string $type = 'Can\'t be empty'; #[IsNotEmpty] #[IsEnum(enum: UserRole::class)] public string $role = 'ValidEnumValue'; #[IsOptional] public string $identifier; public function validate() { Validator::validate($this); } }
Then call validate method, it will throw exception if found an error. Currently this package only support property attribute.
Available Attributes
Available attributes (see src/Attribute folder):
IsEmail(): Email validation (using phpfilter_varfunction).IsEnum(enum: ExampleEnum::class): Enum validation from string value.IsInteger(): Integer validation (using phpis_intfunction).IsNotEmpty(): Validate trimmed value is not empty string or null.IsNumeric(length?: number, minLength?: number, maxLength?: number): Validate numeric string (using phpis_numericfunction).IsOptional(): Attribute to mark the property is optional, allow to be not defined.IsString(length?: number, minLength?: number, maxLength?: number): Validate string length.Name(name: string): Set error message field name (optional, default will be the property name).ValidateArrayClass(type: string): Nested validate array of class.ValidateClass(): Nested validate class.
Error Message
Currently only English and Indonesian error message will thrown (see locale). Default will be English.
To change locale you can use and setLang method in your root or boot project:
<?php declare(strict_types=1); require __DIR__.'/vendor/autoload.php'; use Hsnfirdaus\ClassValidator\Validator; Validator::setLang('id'); // Then you can call Validator::validate method
Or, you can use your own locale with setLangDir method. For example, create your locale file in __DIR__.'/locale/kr.php, then you can use:
Validator::setLangDir(__DIR__.'/locale'); Validator::setLang('kr');
hsnfirdaus/class-validator 适用场景与选型建议
hsnfirdaus/class-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 513 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 06 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php validation」 「php class validator」 「class vaidator」 「php validation attribute」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hsnfirdaus/class-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hsnfirdaus/class-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hsnfirdaus/class-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Collection of methods that I or you usually use.
A very simple yet useful helper class to handle PHP file uploads.
Adds request-parameter validation to the SLIM 3.x PHP framework
A jQuery augmented PHP library for creating and validating HTML forms
Yii2 js base class
Class loader implementation (PSR-0, PSR-4)
统计信息
- 总下载量: 513
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-13