hyperf 验证器问题

2022-04-24 12:44:12 +08:00
 caola

hyperf 的验证器,如何在控制器中获取到验证失败的信息,默认只是获取成功后的验证。

1954 次点击
所在节点    PHP
10 条回复
codefever
2022-04-24 13:24:41 +08:00
安装 validation

composer require hyperf/validation
安装 translation

composer require hyperf/translation
生成 translation 配置文件

php bin/hyperf.php vendor:publish hyperf/translation
生成 validation 配置文件

php bin/hyperf.php vendor:publish hyperf/validation
生成验证器 LoginRequest

php bin/hyperf.php gen:request LoginRequest
控制器 app/Controller/IndexController.php

<?php
namespace App\Controller;

use Hyperf\HttpServer\Annotation\AutoController;
use App\Request\LoginRequest;

/**
* @AutoController();
*/
class IndexController
{
public function index(LoginRequest $request){
$validateData = $request->validated();
return $validateData;
}
}
验证器添加规则 app/Request/LoginRequest.php

<?php

declare(strict_types=1);

namespace App\Request;

use Hyperf\Validation\Request\FormRequest;

class LoginRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'name'=>'required',
'password'=>'required'
];
}
}
添加验证器中间件

<?php

declare(strict_types=1);

return [
'http' => [
\Hyperf\Validation\Middleware\ValidationMiddleware::class
],
];
添加异常处理器

<?php

declare(strict_types=1);

return [
'handler' => [
'http' => [
Hyperf\HttpServer\Exception\Handler\HttpExceptionHandler::class,
App\Exception\Handler\AppExceptionHandler::class,
Hyperf\Validation\ValidationExceptionHandler::class
],
],
];
测试 1

curl 118.195.173.53:9501/index/index
name 字段是必须的
测试 2

curl 118.195.173.53:9501/index/index?name=huyongjian
password 字段是必须
测试 3

curl 118.195.173.53:9501/index/index?name=huyongjian\&password=123456
{
"name": "huyongjian",
"password": "123456"
}
codefever
2022-04-24 13:25:22 +08:00
@codefever 忘了说,教程来自老鄢博客
caola
2022-04-24 13:47:42 +08:00
@codefever 你是没理解我的意思啊,我是要在控制器里,获取验证不通过时的提示信息;
比如用户名是必填的,那么就得获取 ->errors()->first() 第一个必填的提示信息。但这个只能是自定义手动创建验证器才能实现。
我是想在不改为原本的验证器的情况下,拿到这个验证失败的信息,之后再完成其他操作
abigeater
2022-04-24 13:54:37 +08:00
通过捕捉异常可以获取到验证失败的信息
abigeater
2022-04-24 13:55:59 +08:00
补 4 楼,敲了一下就回复了,还没来得及贴文档地址: https://hyperf.wiki/2.2/#/zh-cn/exception-handler
caola
2022-04-24 14:11:29 +08:00
@abigeater 定义异常处理器,怎么把信息抛回到控制器里,我不需要打印输出。
只要放在一个变量里或者方法里,在控制能获取到就行
abigeater
2022-04-24 14:33:41 +08:00
@caola Hyperf 的验证器是通过中间件实现的,在中间件已经被拦截抛异常出去了,走不进控制器层。
想要在控制器层只能是手动创建验证器,或者修改原有的中间件,出错后不要抛异常,将异常存在上下文,走进控制器后从上下文拿到,再自己控制是否抛异常。
AngryPanda
2022-04-24 16:13:52 +08:00
@abigeater #7 为啥我感觉这样的设计不是很合理?很多验证规则可能是与业务绑定的。这样的设计如何解决此类问题?
abigeater
2022-04-24 16:58:43 +08:00
@AngryPanda 我也觉得很多验证规则是和业务绑定的,所以我在这层验证器一般只做参数必填和数据结构的校验,并且比较复杂的结构下 Laravel 验证器的效率已被验证较低;
limingxinleo
2022-04-25 09:25:21 +08:00
@abigeater 这位同学说的很对了,

另外就是如果你不想让框架捕获异常,可以直接在 控制器里,手动 new 验证器,然后自己验证,相关代码可以去看看 ValidationMiddleware 里的实现

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/848919

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX