yangxiaocan/webman-validate

webman validate

dev-master 2021-11-22 13:49 UTC

This package is not auto-updated.

Last update: 2024-12-18 02:52:03 UTC


README

基于topthink/think-validate修改用于webman 暂时只是修正部分使用过程的错误供自用

安装

composer require yangxiaocan/webman-validate

用法

use yangxiaocan\facade\Validate;

$validate = Validate::rule([
    'name'  => 'require|max:25',
    'email' => 'email'
]);

$data = [
    'name'  => 'thinkphp',
    'email' => 'thinkphp@qq.com'
];

if (!$validate->check($data)) {
    var_dump($validate->getError());
}

支持创建验证器进行数据验证

<?php
namespace app\index\validate;

use yangxiaocan\Validate;

class User extends Validate
{
    protected $rule =   [
        'name'  => 'require|max:25',
        'age'   => 'number|between:1,120',
        'email' => 'email',    
    ];
    
    protected $message  =   [
        'name.require' => '名称必须',
        'name.max'     => '名称最多不能超过25个字符',
        'age.number'   => '年龄必须是数字',
        'age.between'  => '年龄只能在1-120之间',
        'email'        => '邮箱格式错误',    
    ];
    
}

验证器调用代码如下:

$data = [
    'name'  => 'thinkphp',
    'email' => 'thinkphp@qq.com',
];

$validate = new \app\index\validate\User;

if (!$validate->check($data)) {
    var_dump($validate->getError());
}

更多用法可以参考6.0完全开发手册的验证章节