hyperf / validation
hyperf validation
                                    Fund package maintenance!
                                                                            
                                                                                                                                        Open Collective
                                                                                    
                                                                            
                                                                                                                                        hyperf.wiki/#/zh-cn/donate
                                                                                    
                                                                
Installs: 1 848 467
Dependents: 192
Suggesters: 14
Security: 0
Stars: 12
Watchers: 3
Forks: 10
Open Issues: 1
pkg:composer/hyperf/validation
Requires
- php: >=8.2
 - egulias/email-validator: ^3.0
 - hyperf/collection: ~3.2.0
 - hyperf/conditionable: ~3.2.0
 - hyperf/context: ~3.2.0
 - hyperf/contract: ~3.2.0
 - hyperf/di: ~3.2.0
 - hyperf/framework: ~3.2.0
 - hyperf/macroable: ~3.2.0
 - hyperf/stringable: ~3.2.0
 - hyperf/support: ~3.2.0
 - hyperf/tappable: ~3.2.0
 - hyperf/translation: ~3.2.0
 - nesbot/carbon: ^2.21 || ^3.0
 - psr/container: ^1.0 || ^2.0
 - psr/event-dispatcher: ^1.0
 - psr/http-message: ^1.0 || ^2.0
 
Suggests
- hyperf/database: Required if you want to use the database validation rule (~3.2.0).
 - hyperf/http-server: Required if you want to use the request validation rule (~3.2.0).
 
- 3.2.x-dev
 - v3.2.0-alpha.3
 - v3.2.0-alpha.1
 - 3.1.x-dev
 - dev-master / 3.1.x-dev
 - v3.1.63
 - v3.1.62
 - v3.1.60
 - v3.1.59
 - v3.1.57
 - v3.1.53
 - v3.1.47
 - v3.1.46
 - v3.1.42
 - v3.1.35
 - v3.1.28
 - v3.1.27
 - v3.1.25
 - v3.1.22
 - v3.1.15
 - v3.1.9
 - v3.1.0
 - v3.1.0-rc.13
 - v3.1.0-rc.9
 - v3.1.0-rc.4
 - v3.1.0-rc.3
 - v3.1.0-rc.1
 - v3.1.0-beta.11
 - v3.1.0-beta.7
 - v3.1.0-beta.6
 - v3.1.0-beta.3
 - v3.1.0-alpha.2
 - v3.1.0-alpha.1
 - 3.0.x-dev
 - v3.0.44
 - v3.0.41
 - v3.0.37
 - v3.0.36
 - v3.0.35
 - v3.0.33
 - v3.0.28
 - v3.0.24
 - v3.0.18
 - v3.0.17
 - v3.0.16
 - v3.0.15
 - v3.0.0
 - v3.0.0-rc.21
 - v3.0.0-rc.13
 - v3.0.0-rc.12
 - v3.0.0-rc.10
 - v3.0.0-rc.8
 - v3.0.0-rc.3
 - v3.0.0-rc.1
 - v3.0.0-beta.10
 - v3.0.0-beta.6
 - v3.0.0-beta.5
 - v3.0.0-beta.1
 - v3.0.0-alpha.4
 - v3.0.0-alpha.1
 - 2.2.x-dev
 - v2.2.33
 - v2.2.13
 - v2.2.12
 - v2.2.7
 - v2.2.6
 - v2.2.5
 - v2.2.0
 - v2.2.0-rc2
 - v2.2.0-beta1
 - 2.1.x-dev
 - v2.1.22
 - v2.1.10
 - v2.1.1
 - v2.1.0
 - v2.1.0-beta2
 - v2.1.0-beta1
 - 2.0.x-dev
 - v2.0.24
 - v2.0.22
 - v2.0.17
 - v2.0.13
 - v2.0.9
 - v2.0.8
 - v2.0.5
 - v2.0.3
 - v2.0.0
 - 1.1.x-dev
 - v1.1.32
 - v1.1.28
 - v1.1.26
 - v1.1.22
 - v1.1.17
 - v1.1.16
 - v1.1.13
 - v1.1.11
 - v1.1.9
 - v1.1.4
 - v1.1.3
 - v1.1.2
 - v1.1.1
 - v1.1.0
 - dev-xuanyanwow-patch-1
 
This package is auto-updated.
Last update: 2025-11-04 03:14:58 UTC
README
About
hyperf/validation 组件衍生于 Laravel Validation 组件的,我们对它进行了一些改造,大部分功能保持了相同。在这里感谢一下 Laravel 开发组,实现了如此强大好用的 Validation 组件。
Installation
composer require hyperf/validation
Config
Publish config file
# 发布国际化配置,已经发布过国际化配置可以省略
php bin/hyperf.php vendor:publish hyperf/translation
php bin/hyperf.php vendor:publish hyperf/validation
Configuration path
your/config/path/autoload/translation.php
Configuration
<?php return [ 'locale' => 'zh_CN', 'fallback_locale' => 'en', 'path' => BASE_PATH . '/storage/languages', ];
Exception handler
<?php return [ 'handler' => [ 'http' => [ \Hyperf\Validation\ValidationExceptionHandler::class, ], ], ];
Validation middleware
<?php return [ 'http' => [ \Hyperf\Validation\Middleware\ValidationMiddleware::class, ], ];
Usage
Generate form request
Command:
php bin/hyperf.php gen:request FooRequest
Usage:
class IndexController { public function foo(FooRequest $request) { $request->input('foo'); } public function bar(RequestInterface $request) { $factory = $this->container->get(\Hyperf\Validation\Contract\ValidatorFactoryInterface::class); $factory->extend('foo', function ($attribute, $value, $parameters, $validator) { return $value == 'foo'; }); $factory->replacer('foo', function ($message, $attribute, $rule, $parameters) { return str_replace(':foo', $attribute, $message); }); $validator = $factory->make( $request->all(), [ 'name' => 'required|foo', ], [ 'name.foo' => ':foo is not foo', ] ); if (!$validator->passes()) { $validator->errors(); } } }