PHP code example of xzusoft / validate

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

    

xzusoft / validate example snippets


private $defaultErrorMsg = [
    'activeUrl'  => ':fieldName必须是可访问的网址',
    'alpha'      => ':fieldName只能是字母',
    'between'    => ':fieldName只能在 :arg0 - :arg1 之间',
    'bool'       => ':fieldName只能是布尔值',
    'dateBefore' => ':fieldName必须在日期 :arg0 之前',
    'dateAfter'  => ':fieldName必须在日期 :arg0 之后',
    'equal'      => ':fieldName必须等于:arg0',
    'float'      => ':fieldName只能是浮点数',
    'func'       => ':fieldName自定义验证失败',
    'inArray'    => ':fieldName必须在 :arg0 范围内',
    'integer'    => ':fieldName只能是整数',
    'isIp'       => ':fieldName不是有效的IP地址',
    'notEmpty'   => ':fieldName不能为空',
    'numeric'    => ':fieldName只能是数字类型',
    'notInArray' => ':fieldName不能在 :arg0 范围内',
    'length'     => ':fieldName的长度必须是:arg0',
    'lengthMax'  => ':fieldName长度不能超过:arg0',
    'lengthMin'  => ':fieldName长度不能小于:arg0',
    'max'        => ':fieldName的值不能大于:arg0',
    'min'        => ':fieldName的值不能小于:arg0',
    'regex'      => ':fieldName不符合指定规则',
    '


/**
 * Created by PhpStorm.
 * User: root
 * Date: 18-11-19
 * Time: 上午10:47
 */

lidate\Validate();
$validate->addColumn('name')->ray && $key == 'age' && $params[$key] == 18;
}, '只允许18岁的进入');
$bool = $validate->validate($data); // 验证结果
if ($bool) {
    var_dump("验证通过");
} else {
    var_dump($validate->getError()->__toString());
}
/*
 * 输出结果: string(23) "只允许18岁的进入"
 */


/**
 * Created by PhpStorm.
 * User: Heelie
 * Date: 19-12-27
 * Time: 下午15:52
 */

回当前校验规则的名字
     * @return string
     */
    public function name(): string
    {
        return 'mobile';
    }

    /**
     * 检验失败返回错误信息即可
     *
     * @param \EasySwoole\Spl\SplArray $spl
     * @param string $column
     * @param mixed ...$args
     * @return string|null
     */
    public function validate(\EasySwoole\Spl\SplArray $spl, $column, ...$args): ?string
    {
        $regular = '/^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$/';
        if (!preg_match($regular, $spl->get($column))) {
            return '手机号验证未通过';
        }
        return null;
    }
}

// 待验证数据
$data     = ['mobile' => '12312345678'];
$validate = new \EasySwoole\Validate\Validate();
// 自定义错误消息示例
$validate->addColumn('mobile')->