From eeaa60f16b935fc94b3dc7b46cf86d05fe6402d5 Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 12 Aug 2020 00:58:43 +0800 Subject: [PATCH 1/2] Add not_in_list rule --- system/Language/en/Validation.php | 1 + system/Validation/Rules.php | 26 ++++++++++++++++-------- tests/system/Validation/RulesTest.php | 29 +++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/system/Language/en/Validation.php b/system/Language/en/Validation.php index 73a38c2059df..2f3dc0c8b101 100644 --- a/system/Language/en/Validation.php +++ b/system/Language/en/Validation.php @@ -48,6 +48,7 @@ 'max_length' => 'The {field} field cannot exceed {param} characters in length.', 'min_length' => 'The {field} field must be at least {param} characters in length.', 'not_equals' => 'The {field} field cannot be: {param}.', + 'not_in_list' => 'The {field} field must not be one of: {param}.', 'numeric' => 'The {field} field must contain only numbers.', 'regex_match' => 'The {field} field is not in the correct format.', 'required' => 'The {field} field is required.', diff --git a/system/Validation/Rules.php b/system/Validation/Rules.php index 9621676c5541..f7dda287441c 100644 --- a/system/Validation/Rules.php +++ b/system/Validation/Rules.php @@ -174,8 +174,7 @@ public function is_not_unique(string $str = null, string $field, array $data): b } } - return (bool) ($row->get() - ->getRow() !== null); + return (bool) ($row->get()->getRow() !== null); } //-------------------------------------------------------------------- @@ -190,10 +189,7 @@ public function is_not_unique(string $str = null, string $field, array $data): b */ public function in_list(string $value = null, string $list): bool { - $list = explode(',', $list); - $list = array_map(function ($value) { - return trim($value); - }, $list); + $list = array_map('trim', explode(',', $list)); return in_array($value, $list, true); } @@ -237,8 +233,7 @@ public function is_unique(string $str = null, string $field, array $data): bool } } - return (bool) ($row->get() - ->getRow() === null); + return (bool) ($row->get()->getRow() === null); } //-------------------------------------------------------------------- @@ -334,6 +329,21 @@ public function not_equals(string $str = null, string $val): bool //-------------------------------------------------------------------- + /** + * Value should not be within an array of values. + * + * @param string $value + * @param string $list + * + * @return boolean + */ + public function not_in_list(string $value = null, string $list): bool + { + return ! $this->in_list($value, $list); + } + + //-------------------------------------------------------------------- + /** * Required * diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index 6081a217e653..7325b2b7ecbd 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -1319,8 +1319,9 @@ public function lessThanEqualProvider() /** * @dataProvider inListProvider * - * @param $str - * @param $expected + * @param string $first + * @param string $second + * @param boolean $expected */ public function testInList($first, $second, $expected) { @@ -1337,6 +1338,30 @@ public function testInList($first, $second, $expected) //-------------------------------------------------------------------- + /** + * @dataProvider inListProvider + * + * @param string $first + * @param string $second + * @param boolean $expected + */ + public function testNotInList($first, $second, $expected) + { + $expected = ! $expected; + + $data = [ + 'foo' => $first, + ]; + + $this->validation->setRules([ + 'foo' => "not_in_list[{$second}]", + ]); + + $this->assertEquals($expected, $this->validation->run($data)); + } + + //-------------------------------------------------------------------- + public function inListProvider() { return [ From 04e263a2124f5c19f91b748363d197418848b5fe Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 12 Aug 2020 01:02:45 +0800 Subject: [PATCH 2/2] Add not_in_list to user guide [ci skip] --- user_guide_src/source/libraries/validation.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index 9d31d27e295e..7dda0a5aed28 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -781,6 +781,7 @@ less_than_equal_to Yes Fails if field is greater than the parameter matches Yes The value must match the value of the field in the parameter. matches[field] max_length Yes Fails if field is longer than the parameter value. max_length[8] min_length Yes Fails if field is shorter than the parameter value. min_length[3] +not_in_list Yes Fails if field is within a predetermined list. not_in_list[red,blue,green] numeric No Fails if field contains anything other than numeric characters. regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/] permit_empty No Allows the field to receive an empty array, empty string, null or false. @@ -829,7 +830,7 @@ Rule Parameter Description ======================= =========== =============================================================================================== ======================================== uploaded Yes Fails if the name of the parameter does not match the name of any uploaded files. uploaded[field_name] max_size Yes Fails if the uploaded file named in the parameter is larger than the second parameter in max_size[field_name,2048] - kilobytes (kb). Or if the file is larger than allowed maximum size declared in + kilobytes (kb). Or if the file is larger than allowed maximum size declared in php.ini config file - ``upload_max_filesize`` directive. max_dims Yes Fails if the maximum width and height of an uploaded image exceed values. The first parameter max_dims[field_name,300,150] is the field name. The second is the width, and the third is the height. Will also fail if