当前位置:网站首页>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
validateMandatory Verifier class namesceneNon essential election Scene selectioneventIn what event should the data be checked [ Non essential election ] Default :creating,updating,saving
边栏推荐
- Differences between overloads, rewrites, abstract classes and interfaces
- 自定义 cube-ui 弹出框dialog支持多个且多种类型的input框
- Socket. Io long Connection Push, version Control, Real - Time Active user volume Statistics
- 19 fonctions de perte d'apprentissage profond
- 重载,重写的区别,抽象类,接口的区别
- Prime mover × Cloud primordial is making sound, reducing cost and increasing efficiency lecture hall
- Filecoin hacker song developer competition
- Apple MDM bypass jailfree bypass MDM configuration lock free
- What is webrtc?
- Jenkins continuous integration 1
猜你喜欢

Apple MDM bypass jailfree bypass MDM configuration lock free

Difficulty calculation of Ethereum classic

Slow content advertising: the long-term principle of brand growth
![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]

Xcode13.3.1 项目执行pod install后报错

High quality domestic stereo codec cjc8988, pin to pin replaces wm8988

Filecoin hacker song developer competition

socke.io长连接实现推送、版本控制、实时活跃用户量统计

What are the advantages of e-mail marketing? Why do sellers of shopline independent station attach so much importance to it?

How the third-party libraries in cocoapod reference local header files
随机推荐
Typescript interface
重载,重写的区别,抽象类,接口的区别
5G网络整体架构
sql及list去重操作
Shanghai Yuge ASR CAT1 4G module 2-way low power 4G application
YYGH-BUG-02
Lombok @equalsandhashcode annotation how to make objects The equals () method compares only some attributes
Differences between overloads, rewrites, abstract classes and interfaces
数据中台:数据治理的七把利剑
深度学习19种损失函数
Windows环境Redis使用AOF持久化,无法生成AOF文件,生成后无法加载AOF文件内容
Filecoin黑客松开发者大赛
Sharing tips for efficient scripting
Small ball playing
Yolact++ Pytorch环境
YYGH-BUG-03
PKG package node project (express)
Xcode13.3.1 error reported after pod install
bash install. SH ******** error
mysql常用函数