The validator object
This is the class that will be instantiated to perform validation
use Sirius\Validation\RuleFactory;
use Sirius\Validation\ErrorMessage;
use Sirius\Validation\Validator;
$ruleFactory = new RuleFactory;
$errorMessagePrototype = new ErrorMessage;
$validator = new Validator($ruleFactory, $errorMessagePrototype);
$validatorFactory and $errorMessagePrototype are optional, they have a default value.
See RuleFactory and ErrorMessage for details
Add validation rules
These are just instructions for the RuleFactory to create the actual rules
// syntax
$validator->add($selector, $name = null, $options = null, $messageTemplate = null, $label = null);
// examples
$validator->add('username', 'required');
$validator->add('password', 'minLength', ['min' => 6];, '{label} must have at least {min} characters', 'Password');
$validator->add('additional_emails[*]', 'email', array(), 'Email address is not valid');
Be sure to check the syntactic sugar options to reduce the verbosity.
$selector
Is the path to the value(s) that will be validated with the rule
$name
The $name must either:
- the name of a rule registered with the
RuleFactory - the name of a class within the
Sirius\Validation\Rulenamespace (eg: `Email', 'MinLength') - the name of a class that extends the
Sirius\Vaidation\Rule\AbstractRuleclass - a callable entity (function, object method or static method) (eg:
$validator->add('username', 'MyClass::validateUsername', null, 'Username is already taken')).
$options
The $options variable represents the configuration options for the validators or additional parameters for the callback. It can be:
- an array
- a JSON string:
{"min": 100, "max": 200} - a URL query string:
min=100&max=200 - a CSV string:
100,200(this requires the validation rule class has theoptionsIndexMaparray properly set up)
$messageTemplate
The $messageTemplate is the message that will be associated with an item when the validation fails.
Each validator has it's own default error message so you don't have to provide it.
$label
The $label is the label associated with the field.
The most useful error messages are those that contain the name of the field so this will come very handy.
Validate data
$validationResult = $validator->validate($_POST); // TRUE or FALSE
$messages = $validator->getMessages(); // array with all error messages
$emailErrorMessages = $validator->getMessages('email'); // error messages for the email address
If for whatever reason you need to manually set error messages you can do it like so
$validator->addMessage('email', 'This value should be an email address');
and clear them
$validator->clearMessages();
Anytime you execute $validator->validate($values) the validation messages are cleared (even those set manually).