fastnorth/validator
最新稳定版本:0.0.13
Composer 安装命令:
composer require fastnorth/validator
包简介
FastNorth Validator
README 文档
README
This is a simple data validation library that deals with validating a variety of data structures. It was born out of dissatisfaction with existing libraries that were either very complex and hard to implement, or seemed to have design flaws.
Installation
Simply install through Composer:
composer require fastnorth/validator
Usage
Validating data is a two part process:
- Defining what your data should look like by defining constraints
- Validating that a dataset matches the constraints
The first step is done by creating a Definition, the second by running the data and definition through a Validator.
Definitions
Definitions are specified through a "field" basis. A field can be anything that can be access through Symfony's PropertyAccess Component.
You can either extend Definition or define the constraints on an instance of it:
<?php use FastNorth\Validator\Definition; $definition = new Definition; $definition // Validate [foo] has a minimum string length of 10 characters ->field('[foo]') ->should(new Constraint\StringLength(['min' => 10])) // [bar] has to exist ->field('[bar]') ->required();
Validating
Validation uses the Validator class. It takes a Definition instance in its constructor, and has a validate() method to validate any data to match the definition. This method returns a Validation instance.
<?php use FastNorth\Validator\Definition; $validator = new Validator($definition); // $yourData = ...; $validation = $validator->validate($yourData); if ($validation->passes()) { // Data matched definition } else { // Validation messages are returned as an array per field foreach($validation->getMessages() as $field => $messagesPerField) { foreach($messagesPerField as $message) { echo $message; } } }
统计信息
- 总下载量: 76.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-13