From 89352fcc15f7fb83d926c0eb8091c534dcc08b5b Mon Sep 17 00:00:00 2001 From: Big Ginger Nerd Date: Tue, 15 Oct 2013 14:59:34 +0200 Subject: [PATCH 1/3] Added validator for boolean --- lang/en.php | 3 ++- src/Valitron/Validator.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lang/en.php b/lang/en.php index 36c3358..8e14f78 100644 --- a/lang/en.php +++ b/lang/en.php @@ -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", ); diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 1ee80ca..939df50 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -430,6 +430,23 @@ class Validator return $vtime > $ptime; } + /** + * Validate that a field contains a boolean. + * If $params[0] is true, it also check for string == true. + * + * @param string $field + * @param mixed $value + * @param array $params + * @return bool + */ + protected function validateBoolean($field, $value, $params) + { + if(isset($params[0]) && $params[0] == true && $value == "true") { + $value = true; + } + return (is_bool($value)) ? true : false; + } + /** * Get array of fields and data */ From da56650a52cf02c0ccf65e60ff5ade33b3cf4921 Mon Sep 17 00:00:00 2001 From: Big Ginger Nerd Date: Tue, 15 Oct 2013 15:01:56 +0200 Subject: [PATCH 2/3] Added test --- tests/Valitron/ValidateTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 0c1f41a..cc1c8a6 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -623,6 +623,34 @@ 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()); + } + + public function testBooleanStringValid() + { + $v = new Validator(array('test' => "true")); + $v->rule('boolean', 'test', true); + $this->assertTrue($v->validate()); + } + + public function testBooleanStringInvalid() + { + $v = new Validator(array('test' => 'notrue')); + $v->rule('boolean', 'test', true); + $this->assertFalse($v->validate()); + } } function sampleFunctionCallback($field, $value, array $params) { From 66d62dd96dabfe7fa204fe5f201c015a0b3d2838 Mon Sep 17 00:00:00 2001 From: Martijn van Maasakkers Date: Tue, 15 Oct 2013 21:21:30 +0200 Subject: [PATCH 3/3] Removed silly string = true check --- src/Valitron/Validator.php | 7 +------ tests/Valitron/ValidateTest.php | 14 -------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 939df50..abf0f89 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -432,18 +432,13 @@ class Validator /** * Validate that a field contains a boolean. - * If $params[0] is true, it also check for string == true. * * @param string $field * @param mixed $value - * @param array $params * @return bool */ - protected function validateBoolean($field, $value, $params) + protected function validateBoolean($field, $value) { - if(isset($params[0]) && $params[0] == true && $value == "true") { - $value = true; - } return (is_bool($value)) ? true : false; } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index cc1c8a6..c859550 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -637,20 +637,6 @@ class ValidateTest extends BaseTestCase $v->rule('boolean', 'test'); $this->assertFalse($v->validate()); } - - public function testBooleanStringValid() - { - $v = new Validator(array('test' => "true")); - $v->rule('boolean', 'test', true); - $this->assertTrue($v->validate()); - } - - public function testBooleanStringInvalid() - { - $v = new Validator(array('test' => 'notrue')); - $v->rule('boolean', 'test', true); - $this->assertFalse($v->validate()); - } } function sampleFunctionCallback($field, $value, array $params) {