1. Go to this page and download the library: Download hehex/hehep-hvalidation 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/ */
use hehe\core\hvalidation\Validation;
$hvalidation = new Validation();
$validation->addValidator('自定义验证器别名','hehe\\core\\validate\\BooleanValidate','自定义消息内容');
use hehe\core\hvalidation\Validation;
$result = Validation::number('12',['name'=>23232]);
// result : true or false
use hehe\core\hvalidation\Validation;
$validation = new Validation();
$validate = $validation->createValidator('range',['min'=>10,'max'=>20]);
$result = $validate->validate(20);
// result : true or false
use hehe\core\hvalidation\annotation\RequiredValid;
use hehe\core\hvalidation\annotation\RangeLengthValid;
use hehe\core\hvalidation\annotation\EgtValid;
class IndexController
{
/**
* @var string
* @RequiredValid("不能为空")
* @RangeLengthValid(min=10,max=20)
*/
public $name;
/**
* @var string
* @RequiredValid("不能为空")
* @EgtValid(number=10)
*/
public $age;
}
/**
* 自定义验证器
* Class CommonValidators
* @package common\extend\validators
*/
class CommonValidators
{
// 定义
public static function install()
{
return [
'tel'=>['class'=>'common\extend\validators\TelValidators'],
// 静态方法调用
'ip'=>['class'=>'CallValidator','func'=>'common\extend\validators\CommonValidators@@ip'],
// 对象方法调用
'ip6'=>['class'=>'CallValidator','func'=>'common\extend\validators\CommonValidators@ip6'],
//'ip'=>['class'=>'CallValidator','func'=>[static::class,'ip']],
];
}
public static function ip($value)
{
$valid = preg_match('/^(\d+\.\d+\.\d+\.\d+)$/', $value);
return $valid === 1;
}
public function ip6($value)
{
$valid = preg_match('/^(\d+\.\d+\.\d+\.\d+)$/', $value);
return $valid === 1;
}
}
namespace common\extend\validators;
use hehe\core\hvalidation\base\Validator;
class TelValidator extends Validator
{
public static function install()
{
return [
'tel'=>['class'=>static::class,'其他规则'=>'xxxx'],
];
}
protected $pattern = '/^1[0-9]{10}$/';
protected function validateValue($value,$name = null)
{
$valid = preg_match($this->pattern, $value);
return $valid === 1;
}
}