From 4ed332c66e882d1c36e691d57112e55ec655ad45 Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Tue, 5 Mar 2013 17:54:05 -0600 Subject: [PATCH] update README for bulk rule method --- README.md | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/README.md b/README.md index 1b0ca27..8895bcd 100644 --- a/README.md +++ b/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