Sirius Validation is a library for data validation. It offers:
- validator object
- 45 build-in validation rules. There are validators for strings, array, numbers, emails, URLs, files and uploads
- validation helper to simplify the validation of single values
Out-of-the-box, the library can handle array
s, ArrayObject
s and objects that have implemented the toArray
method.
In order to validate other data containers you must create a DataWrapper
so that the validator be able to extract data from your object.
$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