mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-31 07:01:54 +00:00
tests and code to support adding multiple rules via an array
This commit is contained in:
parent
650db517b4
commit
d41d115ab7
@ -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)
|
public function rule($rule, $fields)
|
||||||
{
|
{
|
||||||
@ -508,5 +508,31 @@ 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) && !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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -396,5 +396,92 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
|
|||||||
$v->rule('dateAfter', 'date', '2013-01-28');
|
$v->rule('dateAfter', 'date', '2013-01-28');
|
||||||
$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('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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user