Merge branch 'add-rules-in-bulk'

This commit is contained in:
Vance Lucas 2013-03-05 18:01:11 -06:00
commit 423e8e389d
4 changed files with 189 additions and 4 deletions

View File

@ -107,6 +107,67 @@ Valitron\Validation::addRule('alwaysFail', function($field, $value, array $param
}, 'Everything you do is wrong. You fail.'); }, '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 ## Running Tests
The test suite depends on the Composer autoloader to load and run the 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 7. Create new Pull Request
8. Pat yourself on the back for being so awesome 8. Pat yourself on the back for being so awesome

View File

@ -1,6 +1,6 @@
{ {
"name": "vlucas/valitron", "name": "vlucas/valitron",
"version": "1.0.2", "version": "1.0.3",
"type": "library", "type": "library",
"description": "Simple, elegant, stand-alone validation library with NO dependencies", "description": "Simple, elegant, stand-alone validation library with NO dependencies",
"keywords": ["validation", "validator", "valid"], "keywords": ["validation", "validator", "valid"],

View File

@ -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) public function rule($rule, $fields)
{ {
@ -549,5 +549,21 @@ class Validator
); );
return $this; 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);
}
}
}
}

View File

@ -417,5 +417,114 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
$v->rule('contains', 'test_string', array('test')); $v->rule('contains', 'test_string', array('test'));
$this->assertFalse($v->validate()); $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());
}
} }