simonisme/validator
Composer 安装命令:
composer require simonisme/validator
包简介
README 文档
README
How to use one validator?
$validator = new IsEmailValidator(); $result = $validator->valid('email@gmail.com'); // $result contains 0 when evrything is ok // or value > 1 otherwise
How to check value with many validators?
validThroughAllValidators() method checks given value in ALL validators.
$collection = new ValidatorsCollection( [ new IsNotNullValidator(), new IsEmailValidator(), ] ); $result = $collection->validThroughAllValidators('email@gmail.com'); $result->isValid(); // returns true if value is passed thought all validators $result->errors(); // returns array of error numbers for each of not-passed validators
validToFirstError() method checks given value in all validators TO FIRST FAIL.
$collection = new ValidatorsCollection( [ new IsNotNullValidator(), new IsEmailValidator(), ] ); $result = $collection->validToFirstError('email@gmail.com'); $result->isValid(); // returns true if value is passed thought all validators $result->errors(); // returns array of error numbers (in this case there will be only single element in array) for each of not-passed validators
How to check array of values?
You can create array full of validators. This array can contain nested validators arrays:
$validators = [ "email" => new IsEmailValidator(), "age" => new IsNumberValidator() ];
This $validators array can be used to validate $data array:
$data = [ "email" => "incorrect email address", "age" => 35 ]; $arrayValidator = mew ArrayValidator(); $result = $arrayValidator->validateArray($validators, $data);
$result variable contains ValidationResult objects.
ValidationResult::errors() returns nested array with error codes.
For more example look into ./tests/unit/ArrayValidatorTest.php
LIST OF VALIDATORS
- IsBoolValidator
- IsDateTimeValidator
- IsDateValidator
- IsEmailValidator
- IsIntegerValidatorTest
- IsNotNullValidator
- IsNullValidator
- IsNumberEqualValidator
- IsNumberGreaterOrEqualValidator
- IsNumberGreaterThanValidator
- IsNumberInExclusiveRangeValidator
- IsNumberInInclusiveRangeValidator
- IsNumberLessOrEqualValidator
- IsNumberLessThanValidator
- IsNumberValidator
- IsSetValidator
- IsStringValidator
- IsTimeValidator
- IsUrlValidator
- IsValueFromSetValidatorTest
- StringLengthValidator
- SetValidator
License
统计信息
- 总下载量: 271
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-15