承接 ayeo/validator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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
  • Email

Feel free to add some more!

统计信息

  • 总下载量: 34.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固