mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
update README for bulk rule method
This commit is contained in:
parent
173ad40b7d
commit
4ed332c66e
60
README.md
60
README.md
@ -107,6 +107,66 @@ array $params) {
|
||||
}, '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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user