PHP code example of baagee / php-params-validator

1. Go to this page and download the library: Download baagee/php-params-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/ */

    

baagee / php-params-validator example snippets




// 单例模式 获取validator对象
$validator  = \BaAGee\ParamsValidator\Validator::getInstance();

try {
    $res = $validator->addRules('order_id', '1234567890', [
        ['number', '订单号不是数字'],
        ['number|min[10]|max[10]', '订单号不是10位数字'],
    ])->validate();
    var_dump($res);
} catch (\BaAGee\ParamsValidator\Base\ParamInvalid $e) {
    die('Error:' . $e);
}

//要验证的数据
$data = [
    'name'        => 'lotly',
    'age'         => '',
    'sex'         => 1,
    'phone'       => 17878787870,
    'birthday'    => '2019-09-23',
    'weight'      => 50.4,
    'password'    => 'password098',
    'regexp'      => 'dsfa78sdgs-',
    'macAddress'  => '00-01-6C-06-A6-29',
    'IdCard'      => '41282319900909121X',
    'email'       => '[email protected]',
    'zip'         => 240990,
    'homePage'    => 'http://sdfgs.com/asdgfsd?fds=43t&sfds=90',
    'ip'          => '234.32.32.90',
    'ext'         => '{"money":342.3}',
    'bankId'      => '621081257000009900',
    'telephone'   => '010-86551122',
    'chinese'     => '溜溜溜溜',
    'alphaDash'   => 'er2454_34t4-knj',
    'plateNo'     => '京Q9087P',
    'alphaNumber' => 'sdgs34t35ad',
    'alpha'       => 'asdgsege',
    'number'      => '234543456',
    'qq'          => '90876543212',
    'optional'    => '87uhu哈',
    'service'=>'400-021-9999'
];
// 数据对应的验证规则
$rules = [
    // 一个参数支持多个验证规则 二维数组
    'age'         => [
        ['integer|optional|default[20]', '年龄不合法1'],
        ['integer|   'bankId'      => ['BankId', '银行卡号不合法'],
    'telephone'   => ['phone|type[land]', '座机号码不合法'],
    'chinese'     => ['chinese|min[2]|max[4]', '不是纯中文或者长度不在2-4范围内'],
    'alphaDash'   => ['alphaDash', '不是字母数字—_-'],
    'plateNo'     => ['plateNo', '车牌号不合法'],
    'alphaNumber' => ['alphaNum', '不是字母数字组合'],
    'alpha'       => ['alpha', '不是纯字母'],
    'number'      => ['number', '不是纯数字'],
    'service'     => ['phone|type[service]', '不是服务热线'],
];
try {
    $data = $validator->batchAddRules($data, $rules)->validate();
    var_dump($data);
} catch (\BaAGee\ParamsValidator\Base\ParamInvalid $e) {
    die('Error:' . $e);
}

// 使用单个验证规则
$stringRule = new \BaAGee\ParamsValidator\Rules\StringRule();
// 验证一个字段是不是6-12位字符串
$res        = $stringRule->setParams(['min' => 6, 'max' => 12])->check('123jk45');
var_dump($res);
if ($res === false) {
    echo '验证失败' . PHP_EOL;
}
// 或者
$res = (new \BaAGee\ParamsValidator\Rules\StringRule(['min' => 6, 'max' => 12]))->check('123jk45');
var_dump($res);
if ($res === false) {
    echo '验证失败' . PHP_EOL;
}



/**
 * Class MyRule 自定义验证规则 验证是否为某个数的倍数
 * 继承\BaAGee\ParamsValidator\Base\RuleAbstract
 */
class MyRule extends \BaAGee\ParamsValidator\Base\RuleAbstract
{
    /**
        实现具体的check方法
     * @param $value
     * @return array|bool|mixed
     */
    public function check($value)
    {
        $value = intval($value);
        if ($value % $this->params['mo'] !== 0) {
            return false;
        }
        return ['data' => $value];
    }
}

$validator = \BaAGee\ParamsValidator\Validator::getInstance();

try {
    // 注意自定义规则处理类的命名空间,要传完全限定名字
    $validator->addMyRule('num', 29, sprintf('%s|mo[5]', MyRule::class), 'num不是5的倍数');
    $res = $validator->validate();
    var_dump($res);
} catch (\BaAGee\ParamsValidator\Base\ParamInvalid $exception) {
    // 获取出错的参数信息 验证规则,参数名 ,参数值
    echo sprintf('rule: %s ; field: %s ; value: %s ;' . PHP_EOL, $exception->getRule(), $exception->getField(), $exception->getValue());
    die('Error:' . $exception);
}

AlphaDashRule.php
AlphaNumRule.php
AlphaRule.php
BankIdRule.php
ChineseRule.php
DateRule.php
EmailRule.php
EnumRule.php
EqualRule.php
FloatRule.php
IdCardRule.php
IntegerRule.php
IpRule.php
JsonRule.php
MacRule.php
NumberRule.php
PhoneRule.php
PlateNoRule.php
QqRule.php
RegexpRule.php
StringRule.php
UrlRule.php
ZipRule.php