tegic/hyperf-validation-extra
最新稳定版本:v3.0.1
Composer 安装命令:
composer require tegic/hyperf-validation-extra
包简介
README 文档
README
场景规则覆盖、注解
安装
composer require tegic/hyperf-validation-extra
使用
验证器 app/Validate/TestValidate.php
<?php declare(strict_types=1); /** * This file is part of Backend Skeleton. * * @Auther tegic * @Contact teg1c@foxmail.com */ namespace App\Validate; class TestValidate { public function rules(): array { return [ 'user_id' => 'required|integer' ]; } /** * Get custom messages for validator errors. */ public function messages(): array { return [ 'user_id.required' => '登录信息不存在', ]; } public function scenes() { return [ 'test' => [ 'user_id', 'password' => 'required',//这里新增了一个验证规则,底层会自动新增 ], ]; } }
注解调用 app/Controller/IndexController.php
<?php declare(strict_types=1); /** * This file is part of Backend Skeleton. * * @Auther tegic * @Contact teg1c@foxmail.com */ namespace App\Controller; use App\Validate\TestValidate; use Tegic\HyperfValidationExtra\Annotation\Validation; class IndexController extends AbstractController { /** * 不指定参数 默认验证数据从 `$this->request->all()` 获取,场景值为 `test` * @return \Psr\Http\Message\ResponseInterface */ #[Validation(validate: TestValidate::class,field: "",scene: "test")] public function index() { $user = $this->request->input('user', 'Hyperf'); $method = $this->request->getMethod(); $this->test($this->request->all()); return $this->success($this->request->all()); } /** * 指定参数为 `params` 参数为数组形式 * @param $params */ #[Validation(validate: TestValidate::class,field: "params",scene: "test")] protected function test($params) { } }
注意
需要自行捕获异常 Hyperf\Validation\ValidationException
if ($throwable instanceof ValidationException) {
$message = $throwable->validator->errors()->first();
}
统计信息
- 总下载量: 9.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-08