From d41d115ab7a0dd4fed91a9f63a9be38c5602f5b7 Mon Sep 17 00:00:00 2001 From: Joel Clermont Date: Mon, 4 Mar 2013 21:06:04 -0600 Subject: [PATCH] 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()); + } + }