Merge pull request #105 from thinkjson/foreach-discrete

Validate an array of discrete values
This commit is contained in:
Vance Lucas 2014-09-08 14:55:11 -05:00
commit 5bc52a8a39
2 changed files with 34 additions and 0 deletions

View File

@ -803,6 +803,11 @@ class Validator
protected function getPart($data, $identifiers) protected function getPart($data, $identifiers)
{ {
// Catches the case where the field is an array of discrete values
if (is_array($identifiers) && count($identifiers) === 0) {
return array($data, false);
}
$identifier = array_shift($identifiers); $identifier = array_shift($identifiers);
// Glob match // Glob match

View File

@ -308,6 +308,35 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testForeachDiscreteValues()
{
$v = new Validator(array('values' => array(5, 10, 15, 20, 25)));
$v->rule('integer', 'values.*');
$this->assertTrue($v->validate());
}
public function testForeachAssocValues()
{
$v = new Validator(array('values' => array(
'foo' => 5,
'bar' => 10,
'baz' => 15
)));
$v->rule('integer', 'values.*');
$this->assertTrue($v->validate());
}
public function testForeachAssocValuesFails()
{
$v = new Validator(array('values' => array(
'foo' => 5,
'bar' => 10,
'baz' => 'faz'
)));
$v->rule('integer', 'values.*');
$this->assertFalse($v->validate());
}
public function testForeachArrayAccess() public function testForeachArrayAccess()
{ {
$v = new Validator(array('settings' => array( $v = new Validator(array('settings' => array(