PHP code example of easyswoole / validate

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

    

easyswoole / validate example snippets




 = ['name' => 'blank', 'age' => 25];   // 验证数据
$validate = new \EasySwoole\Validate\Validate();
$validate->addColumn('name')->return ($validate->getVerifyData() instanceof SplArray) && $column === 'callback' && $itemData === 0.001;
},'只允许18岁的进入');
$bool = $validate->validate($data); // 验证结果
if ($bool) {
    var_dump("验证通过");
} else {
    var_dump($validate->getError()->__toString());
}
/*
 * 输出结果: string(23) "只允许18岁的进入"
 */



 CustomValidator extends \EasySwoole\Validate\Functions\AbstractValidateFunction
{
    /**
     * 返回当前校验规则的名字
     */
    public function name(): string
    {
        return 'mobile';
    }

    /**
     * 失败在里面做异常也可以
     * @param $itemData
     * @param $arg
     * @param $column
     * @return bool
     */
    public function validate($itemData, $arg, $column, \EasySwoole\Validate\Validate $validate): bool
    {
        $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, $itemData)) {
            return false;
        }

        return true;
    }
}
// 待验证数据
$data     = ['mobile' => '12312345678'];
$validate = new \EasySwoole\Validate\Validate();
// 先添加function 第一个参数为类 第二个参数 是否覆盖 当存在相同名字的验证规则 参数true会替换掉
$validate->addFunction(new CustomValidator(),false);
// 自定义错误消息示例
$validate->addColumn('mobile')->



$validate = new \EasySwoole\Validate\Validate();
// * 可以放在任意位置 且有多个
$validate->addColumn('*.a')->mp("验证通过");
} else {
    var_dump($validate->getError()->__toString());
}
/*
 * 输出结果 *.a只能在 1 - 10 之间
 */