weiqiangxu / Validator

favorite PHP validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validator, PHP Extension

一个我喜欢的简单的验证器.

快速使用.

use xuweiqiang\validator\Validator;

$rules = [
    'chnname'  => [
        'format' => 'string',
        'required' => true,
        'maxLength' => 10,
        'filter' => ['trim', 'filterSpace'],
        'default' => ''
    ],
    'birthday'   => [
        'format' => 'dateTime',
        'required' => false,
        'layout' => 'Y-m-d',
        'default' => null
    ],
    'contracte_mail' => [
        'format' => 'email',
        'minLength' => 3,
        'maxLength' => 100
    ],
    'phone_number' => [
        'format' => 'phone',
        'maxLength' => 15,
        'filter' => ['trim']
    ],
    'money' => [
        'format' => 'float',
        'max' => 5,
        'min' => 1,
        'layout' => '0.00'
    ],
    'gender' => [
        'format' => 'range',
        'required' => true,
        'range' => ['M', 'W'],
    ],
    'age' => [
        'format' => 'regex',
        'regex' => '/\d+/',
        'required' => false,
    ],
];
$msgs = array(
    'chnname'  => [
        'required' => '姓名不能为空.',
        'maxLength' => '姓名不得超过10个字符.',
    ],
    'birthday'    => [
        'format' => '出生日期格式错误',
    ],
    'contracte_mail'   => [
        'email' => '邮箱格式错误',
    ],
    'gender' => [
        'format' => '性别选项值格式错误',
    ],
    'age' => [
        'format' => '年龄格式错误',
    ],
);
$params = array(
    "chnname" => "jack",
    "birthday" => "2020-12-02 10:36:01",
    "contracte_mail" => "123456@qq.com",
    "phone_number" => "1882623366",
    "money" =>'120',
    'gender' => 'W',
    'age' => '18',
);

$Validator = new Validator();
$result = $Validator->CheckMap($params, $rules, $msgs);
print_r($result);
print_r($Validator->error);


验证规则参数说明

校验格式 是否必填 最大长度 最小长度 最大值 最小值 格式化函数 特殊格式 默认零值
format required maxLength minLength max min filter layout default

支持的验证格式

string | dateTime | email | phone | float | range | regex

默认错误提示

格式 format required maxLength minLength
string 限定字符串格式 字段值不能为空 字段长度超出限制 字段长度小于最小长度

格式 format required
range 字段值超出范围 字段值不能为空

License

Validator is licensed under MIT.

About

favorite PHP validator

License:MIT License


Languages

Language:PHP 100.0%