update README for bulk rule method

This commit is contained in:
Joel Clermont 2013-03-05 17:54:05 -06:00
parent 173ad40b7d
commit 4ed332c66e

View File

@ -107,6 +107,66 @@ array $params) {
}, 'Everything you do is wrong. You fail.'); }, 'Everything you do is wrong. You fail.');
``` ```
## Alternate syntax for adding rules
As the number of rules grows, you may prefer the alternate syntax
for defining multiple rules at once.
```php
$rules = [
'required' => 'foo',
'accepted' => 'bar',
'integer' => 'bar'
];
$v = new Valitron\Validator(array('foo' => 'bar', 'bar' => 1));
$v->rules($rules);
$v->validate();
```
If your rule requires multiple parameters or a single parameter
more complex than a string, you need to wrap the rule in an array.
```php
$rules = [
'required' => [
['foo', 'bar']
],
'length' => [
['foo', 3]
]
];
```
You can also specify multiple rules for each rule type.
```php
$rules = [
'length' => [
['foo', 5],
['bar', 5]
]
];
```
Putting these techniques together, you can create a complete
rule definition in a relatively compact data structure.
You can continue to add individual rules with the `rule` method
even after specifying a rule defnition via an array. This is
especially useful if you are defining custom validation rules.
```php
$rules = [
'required' => 'foo',
'accepted' => 'bar',
'integer' => 'bar'
];
$v = new Valitron\Validator(array('foo' => 'bar', 'bar' => 1));
$v->rules($rules);
$v->rule('min', 'bar', 0);
$v->validate();
```
## Contributing ## Contributing