PHP code example of crazymus / pvalidate

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

    

crazymus / pvalidate example snippets


 

$params = array(
    'name' => 'crazymus',
    'age' => 20
);

$rules = array(
    'name' => array(
        'type' => 'string', // 字符串类型
        'title' => '姓名', // 字段名称
        'ray(
        'type' => 'string',  
        'length' => array('>', 10), // 长度必须大于10  
    )
);

$rules = array(
    'name' => array(
        'type' => 'string',  
        'length' => array( // 长度必须大于10,且小于等于50
            array('>', 10),
            array('<=', 50)
        ), 
    )
);

try {
    $validateParams = \Crazymus\Pvalidate::validate($params, $rules);
    print_r($validateParams);  // 校验后得到的数据
} catch (\Exception $e) {
    $e->getMessage();
}



$rules = array(
    'age' => array(
        'type' => 'number', 
        'title' => '年龄',
        ' array(
        'type' => 'number', 
        'value' => array('>', 18) // 大于18
    )
);

$rules = array(
    'age' => array(
        'type' => 'number', 
        'value' => array(  // 大于等于18,且小于60 
            array('>=', 18),
            array('<', 60)
        )
    )
);




$rules = array(
    'age' => array(
        'type' => 'integer', 
        'title' => '年龄',
        '



$rules = array(
    'ratio' => array(
        'type' => 'float', 
        'title' => '比率',
        '



$rules = array(
    'phone' => array(
        'type' => 'phone', 
        'title' => '手机号',
        '



$rules = array(
    'money' => array(
        'type' => 'money', 
        'title' => '金额',
        '



$rules = array(
    'email' => array(
        'type' => 'email', 
        'title' => '邮箱',
        '



$rules = array(
    'site' => array(
        'type' => 'url', 
        'title' => '网址',
        '



$rules = array(
    'idcard' => array(
        'type' => 'idCard', 
        'title' => '身份证',
        '

 

$rules = array(
    'sex' => array(
        'type' => 'number', 
        'title' => '性别',
        '

 namespace MyApp\Rules;

class MyRule extends \Crazymus\Rule\StringRule
{
    public function validate($value)
    {
        parent::validate($value);

        //TODO 实现你的校验逻辑
        if (!in_array($value, array('music', 'movie', 'book'))) {
            throw new \Crazymus\PvalidateException($this->renderErrorMsg('参数校验失败'));
        }
    }
}

// 添加自定义规则 
\Crazymus\Pvalidate::addRules(array(
    'myRule' => '\MyApp\Rules\MyRule'
));

// 使用自定义规则 
$rules = array(
    'hobby' => array(
        'type' => 'myRule', 
        'title' => '爱好',
        '

 

$rules = array(
    'name' => array(
        'type' => 'string',
        'title' => '姓名',
        '

php composer.phar