<?php
namespace App\Controller;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
class TestController
{
/**
* @Inject()
* @var ValidatorFactoryInterface
*/
protected $validationFactory;
public function test(RequestInterface $request)
{
$validator = $this->validationFactory->make(
$request->all(),
// hello限制值必须存在于[1, 2]
// 错误写法:['hello' => ['required', 'integer', [1, 2]]]
// 正确写法:使用Rule::in
['hello' => ['required', 'integer', Rule::in([1, 2])]]
);
}
}
[Hyperf]Method Hyperf\Validation\Validator::validate1 does not exist
于 2021-10-10 11:45:48 首次发布
这篇博客介绍了如何在Hyperf框架的PHP控制器中利用`ValidatorFactoryInterface`进行请求参数验证。通过`@Inject()`注解注入`ValidatorFactoryInterface`,然后在`test`方法中,针对`RequestInterface`获取的所有数据,创建一个验证规则,特别是使用`Rule::in`确保'hello'字段的值必须在1和2之间。这是一个关于PHP后端开发和Web表单验证的实例。

2099

被折叠的 条评论
为什么被折叠?



