diff --git a/README.md b/README.md index c9a104d..4a9c5e2 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,67 @@ Valitron\Validation::addRule('alwaysFail', function($field, $value, array $param }, '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(); +``` + ## Running Tests The test suite depends on the Composer autoloader to load and run the @@ -128,4 +189,3 @@ before running the tests: 7. Create new Pull Request 8. Pat yourself on the back for being so awesome - diff --git a/composer.json b/composer.json index 3fba12d..c7fc2c6 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "vlucas/valitron", - "version": "1.0.2", + "version": "1.0.3", "type": "library", "description": "Simple, elegant, stand-alone validation library with NO dependencies", "keywords": ["validation", "validator", "valid"], diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index c90fe5a..be4e278 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -524,7 +524,7 @@ class Validator } /** - * Convenience method to add validation rules + * Convenience method to add a single validation rule */ public function rule($rule, $fields) { @@ -549,5 +549,21 @@ class Validator ); return $this; } -} + /** + * Convenience method to add multiple validation rules with an array + */ + public function rules($rules) + { + foreach ($rules as $ruleType => $params) { + if (is_array($params)) { + foreach ($params as $innerParams) { + array_unshift($innerParams, $ruleType); + call_user_func_array(array($this, "rule"), $innerParams); + } + } else { + $this->rule($ruleType, $params); + } + } + } +} diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index be59089..cf66088 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -417,5 +417,114 @@ class ValidateTest extends \PHPUnit_Framework_TestCase $v->rule('contains', 'test_string', array('test')); $this->assertFalse($v->validate()); } + + public function testAcceptBulkRulesWithSingleParams() + { + $rules = array( + 'required' => 'nonexistent_field', + 'accepted' => 'foo', + 'integer' => 'foo' + ); + + $v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz')); + $v1->rules($rules); + $v1->validate(); + + $v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz')); + $v2->rule('required', 'nonexistent_field'); + $v2->rule('accepted', 'foo'); + $v2->rule('integer', 'foo'); + $v2->validate(); + + $this->assertEquals($v1->errors(), $v2->errors()); + } + + public function testAcceptBulkRulesWithMultipleParams() + { + $rules = array( + 'required' => array( + array(array('nonexistent_field', 'other_missing_field')) + ), + 'equals' => array( + array('foo', 'bar') + ), + 'length' => array( + array('foo', 5) + ) + ); + + $v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz')); + $v1->rules($rules); + $v1->validate(); + + $v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz')); + $v2->rule('required', array('nonexistent_field', 'other_missing_field')); + $v2->rule('equals', 'foo', 'bar'); + $v2->rule('length', 'foo', 5); + $v2->validate(); + + $this->assertEquals($v1->errors(), $v2->errors()); + } + + public function testAcceptBulkRulesWithNestedRules() + { + $rules = array( + 'length' => array( + array('foo', 5), + array('bar', 5) + ) + ); + + $v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz')); + $v1->rules($rules); + $v1->validate(); + + $v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz')); + $v2->rule('length', 'foo', 5); + $v2->rule('length', 'bar', 5); + $v2->validate(); + + $this->assertEquals($v1->errors(), $v2->errors()); + } + + public function testAcceptBulkRulesWithNestedRulesAndMultipleFields() + { + $rules = array( + 'length' => array( + array(array('foo', 'bar'), 5), + array('baz', 5) + ) + ); + + $v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo')); + $v1->rules($rules); + $v1->validate(); + + $v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo')); + $v2->rule('length', array('foo', 'bar'), 5); + $v2->rule('length', 'baz', 5); + $v2->validate(); + + $this->assertEquals($v1->errors(), $v2->errors()); + } + + public function testAcceptBulkRulesWithMultipleArrayParams() + { + $rules = array( + 'in' => array( + array(array('foo', 'bar'), array('x', 'y')) + ) + ); + + $v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo')); + $v1->rules($rules); + $v1->validate(); + + $v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo')); + $v2->rule('in', array('foo', 'bar'), array('x', 'y')); + $v2->validate(); + + $this->assertEquals($v1->errors(), $v2->errors()); + } }