From d41d115ab7a0dd4fed91a9f63a9be38c5602f5b7 Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Mon, 4 Mar 2013 21:06:04 -0600 Subject: [PATCH 1/6] tests and code to support adding multiple rules via an array --- src/Valitron/Validator.php | 30 ++++++++++++- tests/Valitron/Validate.php | 87 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 3282e4f..bec31fb 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -483,7 +483,7 @@ class Validator } /** - * Convenience method to add validation rules + * Convenience method to add a single validation rule */ public function rule($rule, $fields) { @@ -508,5 +508,31 @@ 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) && !empty($params)) { + if (is_array($params[0])) { + foreach ($params as $innerParams) { + $this->callRuleWithParams($ruleType, $innerParams); + } + } else { + var_dump($params); + $this->callRuleWithParams($ruleType, $params); + } + } else { + $this->rule($ruleType, $params); + } + } + } + + protected function callRuleWithParams($ruleType, $params) + { + array_unshift($params, $ruleType); + call_user_func_array(array($this, 'rule'), $params); + } +} \ No newline at end of file diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index d70a573..ad6fbc8 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -396,5 +396,92 @@ class ValidateTest extends \PHPUnit_Framework_TestCase $v->rule('dateAfter', 'date', '2013-01-28'); $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('nonexistent_field', 'foo'), + 'equals' => array('foo', 'bar'), + 'length' => 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', 'foo')); + $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) + ), + 'required' => array('missing_field_1', 'missing_field_2') + ); + + $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->rule('required', array('missing_field_1', 'missing_field_2')); + $v2->validate(); + + $this->assertEquals($v1->errors(), $v2->errors()); + } + } From 1d68384f05e33c626c56c2fe14e9509520ca7a16 Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Tue, 5 Mar 2013 06:48:47 -0600 Subject: [PATCH 2/6] updated tests to more clearly isolate the issue --- tests/Valitron/Validate.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index ad6fbc8..d7f8fce 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -421,7 +421,7 @@ class ValidateTest extends \PHPUnit_Framework_TestCase public function testAcceptBulkRulesWithMultipleParams() { $rules = array( - 'required' => array('nonexistent_field', 'foo'), + 'required' => array('nonexistent_field', 'other_missing_field'), 'equals' => array('foo', 'bar'), 'length' => array('foo', 5) ); @@ -431,7 +431,7 @@ class ValidateTest extends \PHPUnit_Framework_TestCase $v1->validate(); $v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz')); - $v2->rule('required', array('nonexistent_field', 'foo')); + $v2->rule('required', array('nonexistent_field', 'other_missing_field')); $v2->rule('equals', 'foo', 'bar'); $v2->rule('length', 'foo', 5); $v2->validate(); @@ -466,8 +466,7 @@ class ValidateTest extends \PHPUnit_Framework_TestCase 'length' => array( array(array('foo', 'bar'), 5), array('baz', 5) - ), - 'required' => array('missing_field_1', 'missing_field_2') + ) ); $v1 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo')); @@ -477,7 +476,6 @@ class ValidateTest extends \PHPUnit_Framework_TestCase $v2 = new Validator(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo')); $v2->rule('length', array('foo', 'bar'), 5); $v2->rule('length', 'baz', 5); - $v2->rule('required', array('missing_field_1', 'missing_field_2')); $v2->validate(); $this->assertEquals($v1->errors(), $v2->errors()); From ca553cf65819ac7df1f1b26ad5be548a9bd00f4c Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Tue, 5 Mar 2013 11:27:01 -0600 Subject: [PATCH 3/6] remove debugging code --- src/Valitron/Validator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index bec31fb..9bb36fc 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -521,7 +521,6 @@ class Validator $this->callRuleWithParams($ruleType, $innerParams); } } else { - var_dump($params); $this->callRuleWithParams($ruleType, $params); } } else { From 035de87cf7ce01f04761ee10fc99bf626c93206c Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Tue, 5 Mar 2013 14:14:34 -0600 Subject: [PATCH 4/6] update rules method to be more consistent with rule method --- src/Valitron/Validator.php | 4 +++- tests/Valitron/Validate.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 9bb36fc..9df216b 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -516,7 +516,9 @@ class Validator { foreach ($rules as $ruleType => $params) { if (is_array($params) && !empty($params)) { - if (is_array($params[0])) { + // differentiate between a single rule taking an array of params + // and an array of rules within a single type + if (count($params) > 1 && is_array($params[0]) && is_array($params[1])) { foreach ($params as $innerParams) { $this->callRuleWithParams($ruleType, $innerParams); } diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index d7f8fce..a07b800 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -421,7 +421,7 @@ class ValidateTest extends \PHPUnit_Framework_TestCase public function testAcceptBulkRulesWithMultipleParams() { $rules = array( - 'required' => array('nonexistent_field', 'other_missing_field'), + 'required' => array(array('nonexistent_field', 'other_missing_field')), 'equals' => array('foo', 'bar'), 'length' => array('foo', 5) ); From 173ad40b7dc79c2ef0144ad6bf8dab4869a68546 Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Tue, 5 Mar 2013 17:12:42 -0600 Subject: [PATCH 5/6] fix array structure to make it easier to parse. update tests --- src/Valitron/Validator.php | 19 ++++--------------- tests/Valitron/Validate.php | 30 +++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 9df216b..d6043b4 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -515,25 +515,14 @@ class Validator public function rules($rules) { foreach ($rules as $ruleType => $params) { - if (is_array($params) && !empty($params)) { - // differentiate between a single rule taking an array of params - // and an array of rules within a single type - if (count($params) > 1 && is_array($params[0]) && is_array($params[1])) { - foreach ($params as $innerParams) { - $this->callRuleWithParams($ruleType, $innerParams); - } - } else { - $this->callRuleWithParams($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); } } } - - protected function callRuleWithParams($ruleType, $params) - { - array_unshift($params, $ruleType); - call_user_func_array(array($this, 'rule'), $params); - } } \ No newline at end of file diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index a07b800..bdfd69e 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -421,9 +421,15 @@ class ValidateTest extends \PHPUnit_Framework_TestCase public function testAcceptBulkRulesWithMultipleParams() { $rules = array( - 'required' => array(array('nonexistent_field', 'other_missing_field')), - 'equals' => array('foo', 'bar'), - 'length' => array('foo', 5) + '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')); @@ -481,5 +487,23 @@ class ValidateTest extends \PHPUnit_Framework_TestCase $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()); + } } From 4ed332c66e882d1c36e691d57112e55ec655ad45 Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Tue, 5 Mar 2013 17:54:05 -0600 Subject: [PATCH 6/6] 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