Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add not_in_list rule #3481

Merged
merged 2 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions system/Language/en/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
26 changes: 18 additions & 8 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//--------------------------------------------------------------------
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -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
*
Expand Down
29 changes: 27 additions & 2 deletions tests/system/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 [
Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down