Updated documentation about custom rules

The documentation about custom rules does not specify that creating an
exception for the rule is necessary. This commit will add that to the
documentation with an example to make it explicit.

Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Lee Hesselden 2018-10-24 12:04:06 +01:00 committed by Henrique Moody
parent 66f5475463
commit 780233a44b
No known key found for this signature in database
GPG key ID: 221E9281655813A6

View file

@ -260,6 +260,30 @@ v::with('My\\Validation\\Rules', true);
v::alnum(); // Try to use "My\Validation\Rules\Alnum" if any
```
If you're using the `assert()` or `check()` methods you also need to create an
Exception to declare the messages returned:
```php
namespace My\Validation\Exceptions;
use Respect\Validation\Exceptions\ValidationException;
class MyRuleException extends ValidationException
{
public static $defaultTemplates = [
self::MODE_DEFAULT => [
self::STANDARD => '{{name}} must pass my rules',
],
self::MODE_NEGATIVE => [
self::STANDARD => '{{name}} must not pass my rules',
]
];
}
```
Notice that while the namespace of the rule is `My\Validation\Rules` the
namespace of the exception is `My\Validation\Exceptions`.
## Validator name
On `v::attribute()` and `v::key()`, `{{name}}` is the attribute/key name. For others,