latinosoft/validation

数据验证库(Siriusphp/Validation 更新分支)。使用简单的 API 验证数组、数组对象、领域模型等。轻松添加自己的验证器,在已构建的数十个内置验证规则之上

1.1.3 2020-02-24 04:52 UTC

This package is auto-updated.

Last update: 2024-09-09 03:05:00 UTC


README

#Latinosoft Validation

Latinosoft Validation(Sirius Validation 分支)是一个数据验证库。它提供:

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

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

简介

$validator = new \Latinosoft\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 all of rule's configuration in a string (you'll see later why it's handy')
$validator->add('title:Title', 'maxlength(max=255)({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(array(
    'title:Title' => 'required | maxlength(100)({label} must have less than {max} characters)',
	'content:Content' => 'required',
	'source:Source' => 'website'
));

// 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

链接

已知问题

在 PHP 5.3 中,SplObject 存储存在问题,阻止库删除验证规则。这意味着在 PHP 5.3 中,您无法从 ValidatorValueValidator 对象中删除验证规则。