From 4cc030c68afd3340f983a6bcbadf0cf2c2402cb0 Mon Sep 17 00:00:00 2001 From: Willem Wollebrants Date: Mon, 28 Aug 2017 21:52:21 +0200 Subject: [PATCH] validate fields that are required, but may have empty or null values. #208 --- src/Valitron/Validator.php | 29 ++++++++++++++++++----------- tests/Valitron/ValidateTest.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 4f97fdc..77305fa 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -143,10 +143,16 @@ class Validator * * @param string $field * @param mixed $value + * @param array $params * @return bool */ - protected function validateRequired($field, $value) + protected function validateRequired($field, $value, array $params= array(), array $fields = array()) { + if (isset($params[0]) && (bool) $params[0]){ + $find = $this->getPart($this->_fields, explode('.', $field), true); + return $find[1]; + } + if (is_null($value)) { return false; } elseif (is_string($value) && trim($value) === '') { @@ -886,48 +892,49 @@ class Validator $this->_labels = array(); } - protected function getPart($data, $identifiers) + protected function getPart($data, $identifiers, $allow_empty = false) { // Catches the case where the field is an array of discrete values if (is_array($identifiers) && count($identifiers) === 0) { return array($data, false); } - // Catches the case where the data isn't an array or object if (is_scalar($data)) { return array(NULL, false); } - $identifier = array_shift($identifiers); - // Glob match if ($identifier === '*') { $values = array(); foreach ($data as $row) { - list($value, $multiple) = $this->getPart($row, $identifiers); + list($value, $multiple) = $this->getPart($row, $identifiers, $allow_empty); if ($multiple) { $values = array_merge($values, $value); } else { $values[] = $value; } } - return array($values, true); } - // Dead end, abort elseif ($identifier === NULL || ! isset($data[$identifier])) { + if ($allow_empty){ + //when empty values are allowed, we only care if the key exists + return array(null, array_key_exists($identifier, $data)); + } return array(null, false); } - // Match array element elseif (count($identifiers) === 0) { + if ($allow_empty){ + //when empty values are allowed, we only care if the key exists + return array(null, array_key_exists($identifier, $data)); + } return array($data[$identifier], false); } - // We need to go deeper else { - return $this->getPart($data[$identifier], $identifiers); + return $this->getPart($data[$identifier], $identifiers, $allow_empty); } } diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 2529330..6abdf6e 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -1240,6 +1240,37 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v3->validate()); $this->assertNotEmpty($v3->errors()); } + + public function testRequiredEdgeCases() + { + $v = new Validator(array( + 'zero'=>0, + 'zero_txt' => '0', + 'false'=>false, + 'empty_array'=>array() + )); + $v->rule('required', array('zero', 'zero_txt', 'false', 'empty_array')); + + $this->assertTrue($v->validate()); + } + + public function testRequiredAllowEmpty(){ + $data= array( + 'empty_text'=>'', + 'null_value' => null, + 'in_array'=>array( + 'empty_text'=>'' + ) + ); + + $v1= new Validator($data); + $v1->rule('required', array('empty_text', 'null_value', 'in_array.empty_text')); + $this->assertFalse($v1->validate()); + + $v2= new Validator($data); + $v2->rule('required', array('empty_text', 'null_value', 'in_array.empty_text')); + $this->assertFalse($v2->validate()); + } } function sampleFunctionCallback($field, $value, array $params) {