Updated README.md

This commit is contained in:
Matteo Kloiber 2016-11-06 01:46:34 +01:00
parent 74a882969e
commit ebbeca46af

View File

@ -188,6 +188,29 @@ Valitron\Validator::addRule('alwaysFail', function($field, $value, array $params
}, 'Everything you do is wrong. You fail.');
```
You can also use one-off rules that are only valid for the specified
fields.
```php
$v = new Valitron\Validator(array("foo" => "bar"));
$v->rule(function($field, $value, $params, $fields) {
return true;
}, "foo")->message("{field} failed...");
```
This is useful because such rules can have access to variables
defined in the scope where the `Validator` lives. The Closure's
signature is identical to `Validator::addRule` callback's
signature.
If you wish to add your own rules that are not static (i.e.,
your rule is not static and available to call `Validator`
instances), you need to use `Validator::addInstanceRule`.
This rule will take the same parameters as
`Validator::addRule` but it has to be called on a `Validator`
instance.
## Alternate syntax for adding rules
As the number of rules grows, you may prefer the alternate syntax