title |
---|
The anatomy of a validation rule |
The classes for validation rules extend the Sirius\Validation\Rule\AbstractRule
class
The class has the following constants:
MESSAGE
which is the default message to be shown in case of an error when there is not label associated with the value to be validated.LABELED_MESSAGE
which is use when there is a label associated with the field to be validated
These constants are here so that the validator can return an error message without you having to specify one (see the messageTemplate
property below).
The class has the following properties
options
- the parameters required during validation. In the case of a string length rule the options aremin
(minimum numbers of characters) andmax
(maximum number of characters)messageTemplate
- the string that will determine the error message that will be returned if the value is not valid and you don't want to use the default messagesvalue
- the last value that was validated by the rulecontext
- the data set that is being validated (eg: $_POST in case of a form). Thecontext
must implement theSirius\Validation\DataWrapper\WrapperInterface
The operations below are performed by the RuleFactory but they are here to help you understand the process.
use \Sirius\Validation\Rule\Length as LengthRule;
// we'll use the Length class as an example to check if a string is between 5 and 255 characters long
$validator = new LengthRule(array(
'min' => 5,
'max' => 255
));
// or use the power of your IDE to reduce the chances for mistakes
$validator = new LengthRule(array(
LengthRule::OPTION_MIN => 5,
LengthRule::OPTION_MAX => 255
));
// you can also pass it any string that the class knows how to normalize (see the normalizeOptions() method)
$validator = new LengthRule('min=5&max=255');
// set/override options
$validator->setOption('min', 10);
$validator->setOption('max', 200);
// set the error template message (overwrite the default)
$validator->setMessageTemplate('{label} must be between {min} and {max} characters long');
// add custom options that may be used by the error message
$validator->setOption('label', 'Full name');
// validate a value without a context
$validator->validate('abc'); // this will return false
// or within a context
$validator->validate('abcdefghijlkmnop', $context); // this will return true
// retrieve the error message (instance of `\Sirius\Validation\ErroMessage` which implements toString())
$errorMessage = $validator->getMessage(); // if echo-ed will output 'Full name must be between 10 and 100 characters long'
// if you need to retrieve the potential error message
// (eg: to send the potential error message to a client-side validation library)
$potentialMessage = $validator>getPotentialMessage(); // 'Full name must be between 10 and 100 characters long'