title |
---|
The validation error messages |
The validator object does not return the validation errors as strings but as instances of the Sirius\Validation\ErrorMessage
class.
The reasons for this are:
- most validators have parameters that influence the error message (eg: a validator that requires the value to be a string longer than 10 characters)
- sometimes you need to translate the error messages and each app may have its own way of translating strings.
- we don't know if the translation is done when the validator is constructed or wait until it is displayed to the user
The ErrorMessage
class implements the __toString
method so you can echo them out of the box.
The error message has
- a
template
- a set of
variables
which consists of the rule's arguments (eg: 'min' => 10) and anything else you want
The variables are 'injected' into the resulting string through str_replace
-ing the variables name inside brackets with their values (eg: {min}
is replaced by 10
);
The Sirius\Validation library doesn't use a specific implementation for translating the error messages but we have a recipe for that.