当前位置:网站首页>death_ satan/hyperf-validate
death_ satan/hyperf-validate
2022-06-28 06:00:00 【Death Satan】
Hyperf-Validate
be based on hyperf/validation
Realized validate layer
- Model annotation validation data
- Support validation scenarios
install
# -vvv Show composer Runtime details
composer require death_satan/hyperf-validate -vvv
Usage mode
Generate a by command validate. Here I generate a Demo
php bin/hyperf.php gen:validate Demo
After implementation, it will be in your project app/validate Create one in the directory Demo Verifier class
Here I modify the rules and scenarios I need
<?php
declare(strict_types=1);
namespace App\Validate;
use DeathSatan\Hyperf\Validate\Lib\AbstractValidate as BaseValidate;
class Demo extends BaseValidate
{
/** * @var array Custom Scene */
protected $scenes =[
'login'=>[
'username','password'
], // Write a login scene Check in this scenario username,password Two fields
'reg'=>[
'username',
],// Write a reg scene In this scenario, only check username Field
];
/** * Custom error messages * @return array */
protected function messages():array
{
return [
'username.required'=>' The nickname must be filled in ',
'password.required'=>' The password must be filled in '
];
}
/** * Custom validation properties * @return array */
protected function attributes():array
{
return [];
}
/** * The rules * @return array */
protected function rules():array
{
return [
'username'=>'required',
'password'=>'required',
];
}
}
Used in the controller
<?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();// Take out all the parameters
try {
// adopt scene Method to specify the scene , If you do not use the scenario, you can do without calling
$this->validate->scene('login')
->make($data);
}catch (ValidateException $exception) // If the calibration fails, an error will be thrown ValidateException abnormal
{
return $response->json([
'msg'=>$exception->getMessage(),// adopt getMessage Method to get error information
]);
}
return $response->json([
'msg'=>'success'
]);
}
}
test
stay model Use in
Create a new table
adopt gen:model user
Generate model, stay model Use annotation inside
<?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'];
}
Write relevant codes in the controller
<?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();// Take out all the parameters
try {
User::create($data);// Use it directly create Method
}catch (ModelValidateException $exception) // If the calibration fails, an error will be thrown ModelValidateException abnormal
{
return $response->json([
'msg'=>$exception->getMessage(),// adopt getMessage Method to get error information
]);
}
return $response->json([
'msg'=>'success'
]);
}
}
test
Look at the data sheet
ModelValidate Detailed explanation of annotation parameters
validate
Mandatory Verifier class namescene
Non essential election Scene selectionevent
In what event should the data be checked [ Non essential election ] Default :creating,updating,saving
边栏推荐
- Mysql-17- create and manage tables
- What is webrtc?
- 简单手写debounce函数
- 6. 毕业设计温湿度监控系统(ESP8266 + DHT11 +OLED 实时上传温湿度数据给公网服务器并在OLED显示屏上显示实时温湿度)
- Filecoin黑客松开发者大赛
- Ethereum Classic的难度计算|猿创征文
- Xcode13.3.1 项目执行pod install后报错
- Maskrcnn, fast RCNN, fast RCNN excellent video
- Slow content advertising: the long-term principle of brand growth
- Capacity scheduling absolute value configuration queue usage and pit avoidance
猜你喜欢
随机推荐
YYGH-BUG-02
Filecoin黑客松开发者大赛
UICollectionViewDiffableDataSource及NSDiffableDataSourceSnapshot使用介绍
使用SSM框架,配置多个数据库连接
脚本语言和编程语言
Shanghai Yuge ASR CAT1 4G module 2-way low power 4G application
Install fmpefg
bash install.sh ********错误
lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性
函数栈帧的创建和销毁
5G网络整体架构
移动广告发展动向:撬动存量,精细营销
重载,重写的区别,抽象类,接口的区别
Mysql-16-subquery
慢内容广告:品牌增长的长线主义
Flink window mechanism (two waits and the last explanation)
Jenkins continues integration 2
若依实现下拉框
容量调度绝对值配置队列使用与避坑
Relevant implementation records of CSI and local disk