ayeo/validator
最新稳定版本:1.2.6
Composer 安装命令:
composer require ayeo/validator
包简介
Universal and independent validator
关键字:
README 文档
README
Independent library allows to simple validation other objects
Install
Using composer
composer require ayeo/validator
Example objects
Let's consider simplified objects as below
class Company { /** @var Address */ private $address; /** @var string */ public $name; /** @var Address */ public function getAddress() { return $this->address(); } }
class Address { /** @var string */ public $street; /** @var string */ public $town; /** @var string */ public $countries; }
Validation rules
To process validation we need to define our rules
use Ayeo\Validator\ValidationRules class CompanyValidationRules extends ValidationRules { public function getRules() { return [ ['company', [ ['name', new MinLength(5)], ['address', ['street', new MinLength(5)], ['town', new MinLength(5)], ['country', new OneOf(['USA', 'UK', 'Poland'])] ] ] ]; } }
It is not too sophisticated but works just fine. As you can see we are able to validate nested objects. Validator is smart enough to get private and protected properties (if we got getter). Validator usage:
$company = new Company; $company->name = "Test Company"; $validator = new Validator(new CompanyValidationRules); $isValid = $validator->validate($company); $errors = $validator->getErrors();
Default values
Version 1.2 introduced default values support. In order to set default value you need to pass it as third argument. Default value will be used only if field value is null. Be aware that default value as still subject of further validation - if you set invalid default value it will result with error
use Ayeo\Validator\ValidationRules class CompanyValidationRules extends ValidationRules { public function getRules() { return [['company', [['name', new MinLength(5), "Unknown name"]]]; } }
Allow null
By default given validator will skip checking in case of null value. Of course you need some of them to check even null value. If constraint class implements CheckNull interface validator will force check field even if it is null. At the moment only NotNull constraint it one of this kind.
Availaible constraints
- Length
- MinLength
- MaxLength
- Integer
- Numeric
- NumericMin
- NumericMax
- NotNull
- NonEmpty
- ArrayOfType
- IsArray
- ClassInstance
- NotClassInstance
- LowerThanField
- NoWhitespace
Feel free to add some more!
统计信息
- 总下载量: 34.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-28