The validators ignores null values and empty strings. If you like to make sure a value is not empty, use the NotEmptyValidator.
Multiple validators
All must be true
If you want to check if multiple conditions apply, use the ValidationCombo class. It can join any number and validators and act as a validator itself (so it can also be use in other ValidationCombo).
Only one need to pass
If only one of validators is enough for the value to be valid, you may use the ValidationChoice class instead.
Recursive validation
To apply a validator to every element in an array, use the RecursiveValidator class. It receives one validator in the constructor and checks the elements with such validator.
Usage with exceptions
To throw an exception on a validation error, use the assert method instead of validate. It throws a ValidatorException with a custom message for each validator.
Custom messages
The message of the exception can be set on the validator using the method setErrorMessage(). If the validator is a Combo, then the message setted with this method will be the message shown to any validation error (to avoid this behavior, set the messages of the validators inside the combo).
Each validator has a set of values that can be used in the message. The value named key exists in all validator and defaults to "value". Those keys replace the sequence %key% inside the message. To override a default value of a key, use the addKeyValue method (this can be used to set the key as the name of the form field, for example).
The ValidationSet class can be used as a shortcut to test all the values in an array or the properties of an object all at once. The usage is pretty straightforward:
Partial validation
If you need to validate only a subset of fields (for, say, using in the onBlur event of an input element, without need to validate the whole form), set the second argument of validate (or assert) to the field or list of fields you want to actually validate. If there’s no rules for a given field, it’ll just be ignored.
It’s also possible to use another key or attribute as the input value for a validator (e.g. validate if one field is equal to another). To do that, you need to use two other classes combined: ValidationValue and ValidationKey.
ValidationValue should be included in a set as it were a validator. Its constructor requires a validator as the first argument (can be a string with a FQCN or a dummy validator object) and the arguments to the validator constructor must follow it. To use a field from the validated object in the constructor, pass it as a ValidationKey object with it’s key property being the name of the attribute or key you want to grab from the value being validated.
Let’s make that clear with and example. To make sure the value of key1 is strictly equal to the value of key2 inside the same array, do like the following code:
Keep in mind that the values that are passed to the validator are not checked in the ValidationValue constructor. So if there’s something wrong, it’ll only cause an error when the validation is taking place and the real Validator is constructed.
Note: this doesn’t work with ValidationCombo, but since you can add multiple rules to the same key, this should not be a problem.
Sets and exceptions
With a ValidationSet, exception messages work in a different way than with a Validator. The main exception has no message attached, but it contains an array of errors with the keys being the validated array keys (or object properties) and the values being the child validator exception.
Also, the key name you add to the set is also setted as the %key% template value in the error messages. To change that to another value, use the third argument of addRule() and addRules() methods with the value you want). This can be used if you want to change the language of the message or to specify a more user friendly form label.
Example:
Nested arrays
To validate arrays inside arrays, it is possible to nest ValidationSet as a rule. So, in theory, there’s no limit to how much levels you can nest.
Example:
Api
For the API documentation for all the classes, check the versions page.