From 272fe84b156d5afa3905afe6a27ab0fefd8d739c Mon Sep 17 00:00:00 2001 From: Mark Cahill Date: Tue, 27 May 2014 12:23:34 -0400 Subject: [PATCH] Run validation is field is non-null instead of truthy. Fixes #70 --- src/Valitron/Validator.php | 2 +- tests/Valitron/ValidateTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index bd669cb..ee99b4f 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -698,7 +698,7 @@ class Validator $value = isset($this->_fields[$field]) ? $this->_fields[$field] : null; // Don't validate if the field is not required and the value is empty - if ($v['rule'] !== 'required' && !$this->hasRule('required', $field) && $value == '') { + if ($v['rule'] !== 'required' && !$this->hasRule('required', $field) && (! isset($value) || $value === '')) { continue; } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index e4021ff..ebd4fc9 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -727,6 +727,20 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testZeroStillTriggersValidation() + { + $v = new Validator(array('test' => 0)); + $v->rule('min', 'test', 1); + $this->assertFalse($v->validate()); + } + + public function testFalseStillTriggersValidation() + { + $v = new Validator(array('test' => FALSE)); + $v->rule('min', 'test', 5); + $this->assertFalse($v->validate()); + } + public function testCreditCardValid() { $visa = array(4539511619543489, 4532949059629052, 4024007171194938, 4929646403373269, 4539135861690622);