1. Go to this page and download the library: Download lujihong/easy-validator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lujihong / easy-validator example snippets
declare(strict_types=1);
namespace Api\Controller;
use Hyperf\EasyValidator\Annotation\Body;
use Hyperf\EasyValidator\Annotation\FormData;
use Hyperf\EasyValidator\Annotation\Query;
use Hyperf\EasyValidator\Annotation\Header;
use Api\Vo\Demo\DemoEditVo; //自动生成的Vo类
#[Controller(prefix: '/demo')]
class DemoController extends AbstractController
{
#[
PostMapping(path: "test"),
FormData(key: "phone|手机号", rule: "quest请求中获取请求参数
$vo->getAttributes();//获取所有属性,包含ip
$vo->getIp();//获取Ip
$vo->getTest();//获取测试字段值,已自动转换成int类型
$vo->getTest1();//获取测试1字段值,已自动转换成string类型
//自定义request参数值
$changeVal = 2;
$vo->setTest($changeVal);
$vo->getTest();//此时值已变为2
//设置其他自定义参数
$vo->setOther(['foo_field' => '测试值']);
$vo->getOther();//获取设置的其他值
}
#[
PostMapping(path: 'fromData'),
FormData(key: "phone|手机号", rule: "
use Hyperf\EasyValidator\Annotation\CustomValidationRule;
#[CustomValidationRule]
class DemoCustomValidationRule
{
/**
* 校验错误则返回错误信息, 正确则返回 true
* @param $attribute
* @param $value
* @return bool|string
*/
public function test($attribute, $value): bool|string
{
if (!preg_match('/^1[3456789]{1}\d{9}$/', trim((string)$value))) {
return '格式不正确';
}
return true;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.