windwalker / validator
Windwalker Validator 包
3.5.23
2019-10-26 15:42 UTC
Requires
- php: >=7.1.3
- symfony/polyfill-mbstring: ^1.8
Requires (Dev)
- windwalker/compare: ~3.0
- windwalker/test: ~3.0
Suggests
- windwalker/compare: Install ~3.0 if you want to use CompareValidator.
- 4.x-dev
- 3.x-dev
- dev-master / 3.x-dev
- 3.5.23
- 3.5.22
- 3.5.21
- 3.5.20
- 3.5.19
- 3.5.18
- 3.5.17
- 3.5.16
- 3.5.15
- 3.5.14
- 3.5.13
- 3.5.12
- 3.5.11
- 3.5.10
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4
- 3.3.2
- 3.3.1
- 3.3
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1
- 3.0.1
- 3.0
- 3.0-beta2
- 3.0-beta
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.2
- 2.1.1
- 2.1
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta2
- 2.0.0-beta1
- 2.0.0-alpha
- dev-test
This package is auto-updated.
Last update: 2024-09-18 14:59:57 UTC
README
Windwalker Validator 是一个简单的接口,帮助验证字符串。
通过 Composer 安装
将以下内容添加到你的 composer.json
文件的 require 块中。
{ "require": { "windwalker/validator": "~3.0" } }
简单验证流程
use Windwalker\Validator\Rule\EmailValidator; $validator = new EmailValidator; $validator->validate('[email protected]'); // bool(true) $validator->validate('sakura'); // bool(false)
可用的验证规则
- AlnumValidator
- BooleanValidator
- ColorValidator
- CreditcardValidator
- EmailValidator
- EqualsValidator
- IpValidator
- NoneValidator
- PhoneValidator
- RegexValidator
- UrlValidator
- CallbackValidator
- CompareValidator
- PhpTypeValidator
Regex 验证器
use Windwalker\Validator\Rule\RegexValidator; $validator = new RegexValidator('^[a-zA-Z0-9]*$', 'i'); $validator->validate('abc_123:978'); // bool(false)
等于验证器
use Windwalker\Validator\Rule\EqualsValidator; $validator = new EqualsValidator('ABC'); $validator->validate('ABC'); // bool(true)
严格模式
$validator = new EqualsValidator(123, true); $validator->validate('123'); // bool(false)
错误信息
$validator->setMessage('This string is not valid'); if (!$validator->validate('sakura')) { throw new \Exception($validator->getError()); }
创建自定义验证器
use Windwalker\Validator\AbstractValidator; class MyValidator extends AbstractValidator { public function test($string) { return (bool) strlen($string); } } $validator = new MyValidator; $validator->validate('foo');
扩展 Regex 验证器
use Windwalker\Validator\Rule\RegexValidator; class MyRegexValidator extends RegexValidator { protected $modified = 'i'; protected $regex = '[a-zA-Z]'; }
组合
匹配所有。
use Windwalker\Validator\ValidatorComposite; $validator = new ValidatorComposite([ AlnumValidator::class, new PhoneValidator ]); $validator->validate('1a2b'); // false $validator->getResults(); // [true, false]
匹配一个。
use Windwalker\Validator\ValidatorComposite; $validator = new ValidatorComposite([ AlnumValidator::class, new PhoneValidator ])->setMode(ValidatorComposite::MODE_MATCH_ONE); $validator->validate('1a2b'); // true $validator->getResults(); // [true, false]
使用方法
$validator->validateOne($value); $validator->validateAll($value);