siriusphp/validation

数据验证库。使用简单的API验证数组、数组对象、领域模型等。轻松在已内置的数十个验证规则之上添加自己的验证器

4.0.0 2023-11-19 15:40 UTC

README

Source Code Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Sirius Validation是一个数据验证库。它提供

  1. 验证器对象
  2. 45个内置验证规则。包括字符串、数组、数字、电子邮件、URL、文件和上传的验证器
  3. 验证助手,用于简化单个值的验证

默认情况下,库可以处理 arrayArrayObject 以及实现了 toArray 方法的对象。为了验证其他数据容器,您必须创建一个 DataWrapper,以便验证器能够从您的对象中提取数据。

简介

$validator = new \Sirius\Validation\Validator;

// add a validation rule
$validator->add('title', 'required');

// add a rule that has a list of options
$validator->add('title', 'length', array('min' => 10, 'max' => 100));
// or use JSON
$validator->add('title', 'length', '{"min": 10, "max": 100}');
// or a URL query string
$validator->add('title', 'length', 'min=10&max=100');
// or, if you know that the validator can CORECTLY parse (ie: understand) the options string
$validator->add('title', 'length', '10,100');

// add a rule with a custom error message
$validator->add('title', 'maxlength', 'max=100', 'Article title must have less than {max} characters');

// add a rule with a custom message and a label (very handy with forms)
$validator->add('title:Title', 'maxlength', 'max=100', '{label} must have less than {max} characters');

// add multiple rules at once (separate using [space][pipe][space])
$validator->add('title:Title', 'required | maxlength(255) | minlength(min=10)');

// add all your rules at once
$validator->add([
        'title:Title' => 'required | maxlength(100)',
        'content:Content' => 'required',
        'source:Source' => 'website'
    ], [
        'content.required' => 'The content field should have a velue'
    ]);

// add nested rules
$validator->add('recipients[*]:Recipients', 'email'); //all recipients must be valid email addresses
$validator->add('shipping_address[city]:City', 'MyApp\Validator\City'); // uses a custom validator to validate the shipping city

链接