Merge pull request #31 from biggingernerd/master

Added validator for boolean
This commit is contained in:
Vance Lucas 2013-11-11 13:05:13 -08:00
commit add2cc92d2
3 changed files with 28 additions and 1 deletions

View File

@ -23,7 +23,8 @@ return array(
'dateFormat' => "must be date with format '%s'",
'dateBefore' => "must be date before '%s'",
'dateAfter' => "must be date after '%s'",
'contains' => "must contain %s"
'contains' => "must contain %s",
'boolean' => "must be a boolean",
);

View File

@ -430,6 +430,18 @@ class Validator
return $vtime > $ptime;
}
/**
* Validate that a field contains a boolean.
*
* @param string $field
* @param mixed $value
* @return bool
*/
protected function validateBoolean($field, $value)
{
return (is_bool($value)) ? true : false;
}
/**
* Get array of fields and data
*/

View File

@ -640,6 +640,20 @@ class ValidateTest extends BaseTestCase
$v->rule('testRule', 'name', array('foo', 'bar'))->message('Invalid name selected.');
$this->assertFalse($v->validate());
}
public function testBooleanValid()
{
$v = new Validator(array('test' => true));
$v->rule('boolean', 'test');
$this->assertTrue($v->validate());
}
public function testBooleanInvalid()
{
$v = new Validator(array('test' => 'true'));
$v->rule('boolean', 'test');
$this->assertFalse($v->validate());
}
}
function sampleFunctionCallback($field, $value, array $params) {