tobento / app-validation
应用验证支持。
Requires
- php: >=8.0
- tobento/app: ^1.0
- tobento/app-migration: ^1.0
- tobento/service-message: ^1.0.1
- tobento/service-validation: ^1.0
Requires (Dev)
- nyholm/psr7: ^1.0
- phpunit/phpunit: ^9.5
- tobento/app-http: ^1.0.8
- tobento/app-translation: ^1.0
- tobento/app-view: ^1.0.1
- vimeo/psalm: ^4.0
Suggests
- tobento/app-http: Support for Http validation
- tobento/app-translation: Support for translating messages
README
应用验证支持。
目录
入门
使用以下命令安装应用验证项目的最新版本。
composer require tobento/app-validation
要求
- PHP 8.0 或更高版本
文档
应用
如果您使用的是骨架,请查看App Skeleton。
您还可以查看App以了解更多关于该应用的一般信息。
Validator Boot
验证器启动执行以下操作
- 安装验证器翻译文件
- 验证器和规则接口实现
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Adding boots $app->boot(\Tobento\App\Validation\Boot\Validator::class); // Run the app $app->run();
验证数据
您可以使用ValidatorInterface::class
验证数据。您可以通过几种方式访问验证器
查看验证服务 - 验证部分,了解更多关于数据验证的一般信息。
使用应用
use Tobento\App\AppFactory; use Tobento\Service\Validation\ValidatorInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // Adding boots $app->boot(\Tobento\App\Validation\Boot\Validator::class); $app->booting(); $validator = $app->get(ValidatorInterface::class); $validation = $validator->validating( value: 'foo', rules: 'alpha|minLen:2', ); // var_dump($validation->isValid()); // bool(true) // Run the app $app->run();
使用自动装配
您还可以在任何由应用解析的类中请求ValidatorInterface::class
。
use Tobento\Service\Validation\ValidatorInterface; class SomeService { public function __construct( protected ValidatorInterface $validator, ) {} }
添加规则
默认情况下,默认规则可用。您可以通过以下方式添加更多规则
use Tobento\App\AppFactory; use Tobento\Service\Validation\RulesInterface; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // Adding boots $app->boot(\Tobento\App\Validation\Boot\Validator::class); // using the app on method: $app->on(RulesInterface::class, function(RulesInterface $rules) { $rules->add(name: 'same', rule: new Same()); }); // Run the app $app->run();
查看规则部分,了解更多关于添加规则的信息。
消息翻译
简单地,安装App Translation包,启动\Tobento\App\Translation\Boot\Translation::class
composer require tobento/app-translation
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // Adding boots $app->boot(\Tobento\App\Translation\Boot\Translation::class); $app->boot(\Tobento\App\Validation\Boot\Validator::class); // Run the app $app->run();
消息将根据配置的翻译器区域设置进行翻译。
默认情况下,默认规则错误消息已翻译为en
和de
。
查看添加翻译部分,了解如何添加翻译。
请确保您已定义资源名称validator
用于规则错误消息,如消息翻译修饰符上配置的。消息参数翻译修饰符使用*
作为资源名称。
Http 验证
Http 要求
以下应用示例显示了http验证的最小要求。
首先,安装App Http包
composer require tobento/app-http
此外,您还可以安装App View包以支持视图
composer require tobento/app-view
接下来,确保以下启动已被定义
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Add directories: $app->dirs() ->dir(realpath(__DIR__.'/../'), 'root') ->dir(realpath(__DIR__.'/../app/'), 'app') ->dir($app->dir('app').'config', 'config', group: 'config') ->dir($app->dir('root').'public', 'public') ->dir($app->dir('root').'vendor', 'vendor'); // HTTP boots: // Required for request validation: $app->boot(\Tobento\App\Http\Boot\Routing::class); $app->boot(\Tobento\App\Http\Boot\RequesterResponser::class); // Required for flashing input and messages: $app->boot(\Tobento\App\Http\Boot\Session::class); // VIEW boots: // Optional boots for view support: $app->boot(\Tobento\App\View\Boot\View::class); $app->boot(\Tobento\App\View\Boot\Form::class); $app->boot(\Tobento\App\View\Boot\Messages::class); // VALIDATION boots: // Default error handler for handling ValidationException. // You may create your own handler or add one with higher priority. $app->boot(\Tobento\App\Validation\Boot\HttpValidationErrorHandler::class); $app->boot(\Tobento\App\Validation\Boot\Validator::class); // Run the app $app->run();
使用验证请求
您可以使用Tobento\App\Validation\Http\ValidationRequest::class
验证请求输入。
默认情况下,如果验证失败,将抛出 Tobento\App\Validation\Exception\ValidationException::class
异常。如果已启动,则由 Tobento\App\Validation\Boot\HttpValidationErrorHandler::class
处理此异常。请参阅 Http 验证错误处理器启动。
use Tobento\App\Validation\Http\ValidationRequest; use Tobento\Service\Validation\ValidationInterface; use Tobento\Service\Requester\RequesterInterface; use Tobento\Service\Responser\ResponserInterface; use Tobento\Service\Routing\RouterInterface; use Psr\Http\Message\ResponseInterface; class ProductController { /** * Store a new product. */ public function store(ValidationRequest $request): ResponseInterface { $validation = $request->validate( rules: [ 'title' => 'required|alpha', ], // You may specify an uri for redirection. redirectUri: '/products/create', // Or you may specify a route name for redirection. redirectRouteName: 'products.create', // If no uri or route name is specified, // it will be redirected to the previous url. // You may specify an error message flashed to the user. errorMessage: 'You have some errors check out the fields for its error message', // You may change the behaviour by a custom validation error handler though. ); // The product is valid, store product e.g. var_dump($validation instanceof ValidationInterface); // bool(true) // The following interfaces are available: var_dump($request->requester() instanceof RequesterInterface); // bool(true) var_dump($request->responser() instanceof ResponserInterface); // bool(true) var_dump($request->router() instanceof RouterInterface); // bool(true) // you may use the responser and router for redirection: return $request->responser()->redirect($request->router()->url('products.index')); } }
您可以查看以下链接以获取其文档
手动处理验证
use Tobento\App\Validation\Http\ValidationRequest; use Psr\Http\Message\ResponseInterface; class ProductController { public function store(ValidationRequest $request): ResponseInterface { $validation = $request->validate( rules: [ 'title' => 'required|alpha', ], // you may disable throwing ValidationException on failure // and handling it by yourself. throwExceptionOnFailure: false, ); if (! $validation->isValid()) { // handle invalid validation. } // ... } }
Http 验证错误处理启动
http 错误处理器启动执行以下操作
- 处理
Tobento\App\Validation\Exception\ValidationException::class
异常。
use Tobento\App\AppFactory; // Create the app $app = (new AppFactory())->createApp(); // Adding boots $app->boot(\Tobento\App\Validation\Boot\HttpValidationErrorHandler::class); $app->boot(\Tobento\App\Validation\Boot\Validator::class); // Run the app $app->run();
当接收到的 HTTP 请求期望 JSON 响应时,错误处理器将返回以下格式的 422 无法处理实体 HTTP 响应
{ "message": "The exception message", "errors": { "email": [ "The email is required." ] } }
否则,错误处理器将使用 Tobento\App\Validation\Exception\ValidationException::class
中定义的 redirectUri
参数返回重定向响应,如果未定义,则使用 Tobento\Service\Uri\PreviousUriInterface::class
uri
此外,您还可以创建一个自定义的 错误处理器 或添加一个 优先级更高的错误处理器,其值为 3000
,如 Tobento\App\Validation\Boot\HttpValidationErrorHandler::class
中定义。
实时验证
进行中...