Skip to content

Commit

Permalink
Merge pull request #2721 from musmanikram/2714-fix-validation-for-arr…
Browse files Browse the repository at this point in the history
…ay-fields

Fix validation for array input fields
  • Loading branch information
lonnieezell authored Mar 22, 2020
2 parents c21509d + 5c8b6ff commit 33b61a7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/Helpers/array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function _array_search_dot(array $indexes, array $array)
? array_shift($indexes)
: null;

if (empty($currentIndex) || (! isset($array[$currentIndex]) && $currentIndex !== '*'))
if ((empty($currentIndex) && intval($currentIndex) !== 0) || (! isset($array[$currentIndex]) && $currentIndex !== '*'))
{
return null;
}
Expand Down
77 changes: 77 additions & 0 deletions tests/system/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,81 @@ public function testTagReplacement()
$this->assertEquals($expected, $errors['Username']);
}

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

/**
* @dataProvider arrayFieldDataProvider
*/
public function testRulesForArrayField($body, $rules, $results)
{
$config = new App();
$config->baseURL = 'http://example.com';

$request = new IncomingRequest($config, new URI(), http_build_query($body), new UserAgent());
$request->setMethod('post');

$this->validation->setRules($rules);
$this->validation->withRequest($request)
->run($body);
$this->assertEquals($results, $this->validation->getErrors());
}

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

public function arrayFieldDataProvider() {
return [
'all_rules_should_pass' => [
'body' => [
'foo' => [
'a',
'b',
'c'
]
],
'rules' => [
'foo.0' => 'required|alpha|max_length[2]',
'foo.1' => 'required|alpha|max_length[2]',
'foo.2' => 'required|alpha|max_length[2]'
],
'results' => []
],
'first_field_will_return_required_error' => [
'body' => [
'foo' => [
'',
'b',
'c'
]
],
'rules' => [
'foo.0' => 'required|alpha|max_length[2]',
'foo.1' => 'required|alpha|max_length[2]',
'foo.2' => 'required|alpha|max_length[2]'
],
'results' => [
'foo.0' => 'The foo.0 field is required.'
]
],
'first_and second_field_will_return_required_and_min_length_error' => [
'body' => [
'foo' => [
'',
'b',
'c'
]
],
'rules' => [
'foo.0' => 'required|alpha|max_length[2]',
'foo.1' => 'required|alpha|min_length[2]|max_length[4]',
'foo.2' => 'required|alpha|max_length[2]'
],
'results' => [
'foo.0' => 'The foo.0 field is required.',
'foo.1' => 'The foo.1 field must be at least 2 characters in length.',
]
]
];
}

//--------------------------------------------------------------------
}

0 comments on commit 33b61a7

Please sign in to comment.