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

Fix validation for multi select #2765

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
15 changes: 13 additions & 2 deletions system/Validation/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,20 @@ public function run(array $data = null, string $group = null, string $db_group =
$rules = $this->splitRules($rules);
}

$value = dot_array_search($rField, $data);
$value = dot_array_search($rField, $data);
$fieldNameToken = explode('.', $rField);

$this->processRules($rField, $rSetup['label'] ?? $rField, $value ?? null, $rules, $data);
if (is_array($value) && end($fieldNameToken) === '*')
{
foreach ($value as $val)
{
$this->processRules($rField, $rSetup['label'] ?? $rField, $val ?? null, $rules, $data);
}
}
else
{
$this->processRules($rField, $rSetup['label'] ?? $rField, $value ?? null, $rules, $data);
}
}

return ! empty($this->getErrors()) ? false : true;
Expand Down
109 changes: 100 additions & 9 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,24 +665,25 @@ public function testRulesForArrayField($body, $rules, $results)

//--------------------------------------------------------------------

public function arrayFieldDataProvider() {
public function arrayFieldDataProvider()
{
return [
'all_rules_should_pass' => [
'body' => [
'body' => [
'foo' => [
'a',
'b',
'c'
]
'c',
],
],
'rules' => [
'rules' => [
'foo.0' => 'required|alpha|max_length[2]',
'foo.1' => 'required|alpha|max_length[2]',
'foo.2' => 'required|alpha|max_length[2]'
'foo.2' => 'required|alpha|max_length[2]',
],
'results' => []
'results' => [],
],
'first_field_will_return_required_error' => [
/*'first_field_will_return_required_error' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just wondering, why this data provider commented ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️ 🤦‍♂️ 🤦‍♂️ My bad. Will create another PR.

Thank you for pointing this out

'body' => [
'foo' => [
'',
Expand Down Expand Up @@ -716,8 +717,98 @@ public function arrayFieldDataProvider() {
'foo.0' => 'The foo.0 field is required.',
'foo.1' => 'The foo.1 field must be at least 2 characters in length.',
]
]
]*/
];
}

//--------------------------------------------------------------------

public function testRulesForSingleRuleWithAsteriskWillReturnNoError()
{
$config = new App();
$config->baseURL = 'http://example.com';

$_REQUEST = [
'id_user' => [
1,
3,
],
'name_user' => [
'abc123',
'xyz098',
],
];

$request = new IncomingRequest($config, new URI(), 'php://input', new UserAgent());
$request->setMethod('post');

$this->validation->setRules([
'id_user.*' => 'numeric',
'name_user.*' => 'alpha_numeric',
]);

$this->validation->withRequest($request)
->run();
$this->assertEquals([], $this->validation->getErrors());
}

//--------------------------------------------------------------------

public function testRulesForSingleRuleWithAsteriskWillReturnError()
{
$config = new App();
$config->baseURL = 'http://example.com';

$_REQUEST = [
'id_user' => [
'1dfd',
3,
],
'name_user' => [
123,
'xyz098',
],
];

$request = new IncomingRequest($config, new URI(), 'php://input', new UserAgent());
$request->setMethod('post');

$this->validation->setRules([
'id_user.*' => 'numeric',
'name_user.*' => 'alpha',
]);

$this->validation->withRequest($request)
->run();
$this->assertEquals([
'id_user.*' => 'The id_user.* field must contain only numbers.',
'name_user.*' => 'The name_user.* field may only contain alphabetical characters.',
], $this->validation->getErrors());
}

//--------------------------------------------------------------------

public function testRulesForSingleRuleWithSingleValue()
{
$config = new App();
$config->baseURL = 'http://example.com';

$_REQUEST = [
'id_user' => 'gh',
];

$request = new IncomingRequest($config, new URI(), 'php://input', new UserAgent());
$request->setMethod('post');

$this->validation->setRules([
'id_user' => 'numeric',
]);

$this->validation->withRequest($request)
->run();
$this->assertEquals([
'id_user' => 'The id_user field must contain only numbers.',
], $this->validation->getErrors());
}

//--------------------------------------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,20 @@ You can use the '*' wildcard symbol to match any one level of the array::
'contacts.*.name' => 'required'
]);

"dot array syntax" can also be useful when you have single dimension array data.
For example, data returned by multi select dropdown::

// The data to test:
'user_ids' => [
1,
2,
3
]
// Rule
$validation->setRules([
'user_ids.*' => 'required'
]);

Validate 1 Value
================================================

Expand Down