当前位置:网站首页>death_satan/hyperf-validate
death_satan/hyperf-validate
2022-06-28 05:54:00 【death撒旦】
Hyperf-Validate
基于hyperf/validation实现的validate层
- 模型注解验证数据
- 支持验证场景
安装
# -vvv显示composer 运行时详情
composer require death_satan/hyperf-validate -vvv
使用方式
通过命令生成一个 validate.我这里生成一个Demo
php bin/hyperf.php gen:validate Demo
执行后会在你的项目 app/validate目录下生成一个Demo验证器类

这里我修改为我需要的规则以及场景
<?php
declare(strict_types=1);
namespace App\Validate;
use DeathSatan\Hyperf\Validate\Lib\AbstractValidate as BaseValidate;
class Demo extends BaseValidate
{
/** * @var array 自定义场景 */
protected $scenes =[
'login'=>[
'username','password'
], // 写一个login场景 在这个场景下校检username,password两个字段
'reg'=>[
'username',
],//写一个reg场景 在这个场景下只校检username字段
];
/** * 自定义错误消息 * @return array */
protected function messages():array
{
return [
'username.required'=>'昵称必须填写',
'password.required'=>'密码必须填写'
];
}
/** * 自定义验证属性 * @return array */
protected function attributes():array
{
return [];
}
/** * 规则 * @return array */
protected function rules():array
{
return [
'username'=>'required',
'password'=>'required',
];
}
}
在控制器中使用
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Validate\Demo;
use DeathSatan\Hyperf\Validate\Exceptions\ValidateException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
/** * @AutoController() */
class Index
{
/** * @Inject * @var Demo */
protected $validate;
public function index(RequestInterface $request, ResponseInterface $response)
{
$data = $request->all();//取出来所有参数
try {
//通过scene方法指定场景,如果不使用场景则不调用即可
$this->validate->scene('login')
->make($data);
}catch (ValidateException $exception) //如果校检失败则会抛出ValidateException异常
{
return $response->json([
'msg'=>$exception->getMessage(),//通过getMessage方法获取错误信息
]);
}
return $response->json([
'msg'=>'success'
]);
}
}
测试



在model中使用
新创建一个表

通过gen:model user生成model, 在model内使用注解标注
<?php
declare (strict_types=1);
namespace App\Model;
use DeathSatan\Hyperf\Validate\Annotation\ModelValidate;
use Hyperf\DbConnection\Model\Model;
/** * @property int $id * @property string $username * @property string $password * @ModelValidate(validate="\App\Validate\Demo",event="creating,updating,saving",scene="login") */
class User extends Model
{
/** * The table associated with the model. * * @var string */
protected $table = 'user';
/** * The attributes that are mass assignable. * * @var array */
protected $fillable = [
'username','password'
];;
/** * The attributes that should be cast to native types. * * @var array */
protected $casts = ['id' => 'integer'];
}
在控制器内写入相关代码
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Model\User;
use App\Validate\Demo;
use DeathSatan\Hyperf\Validate\Exceptions\ValidateException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use DeathSatan\Hyperf\Validate\Exceptions\ModelValidateException;
/** * @AutoController() */
class Index
{
/** * @Inject * @var Demo */
protected $validate;
public function index(RequestInterface $request, ResponseInterface $response)
{
$data = $request->all();//取出来所有参数
try {
User::create($data);//直接使用create方法
}catch (ModelValidateException $exception) //如果校检失败则会抛出ModelValidateException异常
{
return $response->json([
'msg'=>$exception->getMessage(),//通过getMessage方法获取错误信息
]);
}
return $response->json([
'msg'=>'success'
]);
}
}
测试



查看数据表

ModelValidate注解参数详解
validate必选 验证器类名scene非必选 场景选择event要在什么事件内进行数据校检 [非必选] 默认:creating,updating,saving
边栏推荐
- 安装 Ffmpefg
- Flink 窗口机制 (两次等待, 最后兜底)
- 脚本语言和编程语言
- Oracle fundamentals summary
- Introduction to uicollectionviewdiffabledatasource and nsdiffabledatasourcesnapshot
- @Autowired注解为空的原因
- numpy. reshape, numpy. Understanding of transfer
- Small ball playing
- 猿粉猿动力-开发者活动袭!
- JSP connects with Oracle to realize login and registration (simple)
猜你喜欢

高质量国产立体声编解码器CJC8988,Pin to Pin替代WM8988

線條動畫

RL 实践(0)—— 及第平台辛丑年冬赛季【Rule-based policy】
![Video tutorial on website operation to achieve SEO operation [21 lectures]](/img/1f/9ae2ed5bfec5749c764630d1daccea.jpg)
Video tutorial on website operation to achieve SEO operation [21 lectures]
![A full set of excellent SEO tutorials worth 300 yuan [159 lessons]](/img/d7/7e522143b1e6b3acf14a0894f50d26.jpg)
A full set of excellent SEO tutorials worth 300 yuan [159 lessons]

Lhasa accordion

Jenkins continuous integration 1

ES9023音频解码芯片的工作原理

qtcanpool 知 07:Ribbon

JSP connects with Oracle to realize login and registration (simple)
随机推荐
Important basis for ERP software company selection
Flink window mechanism (two waits and the last explanation)
pytorch dataloader的长度 epoch与iteration的区别
Syn retransmission caused by IPVS
cocoapod中的第三方库怎么引用本地头文件
【MYSQL】所有查询表中有2千万数据--sql如何优化
联想混合云Lenovo xCloud,新企业IT服务门户
File foundation - read / write, storage
Lhasa accordion
Jenkins continues integration 2
How to do a good job of dam safety monitoring
How to add live chat in your Shopify store?
At first glance, I can see several methods used by motionlayout
Enum
Qtcanpool knowledge 07:ribbon
Common basic functions of Oracle
JSP connecting Oracle to realize login and registration
Solution of dam safety automatic monitoring system for medium and small reservoirs
Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
学术搜索相关论文