Welcome Guest, Not a member yet? Register   Sign In
form_validation of arrays: what does CI when first array index != 0
#1

[eluser]Unknown[/eluser]
Hello,

i've experienced an issue when validating a post array. For example:

This one works perfect.
Code:
<!-- validation rule -->
$this->form_validation->set_rules('myfield[]', 'Mein Feld', 'trim|strip_tags|required|max_length[10]');
<!-- html -->
<input type="text" name="myfield[]" />
<input type="text" name="myfield[]" />
<input type="text" name="myfield[]" />

But:
Code:
<!-- validation rule -->
$this->form_validation->set_rules('myfield[]', 'Mein Feld', 'trim|strip_tags|required|max_length[10]');
<!-- html -->
<input type="text" name="myfield[27]" />
<input type="text" name="myfield[30]" />
<input type="text" name="myfield[33]" />
This one does not validate correctly. It seems that CI (i'm working with v2.0) isn't able to recognize the fields of the POST-array, if they arent in correct order ([0], [1], [2], ...)

Has anyone had the same problem or already got a workaround for it?

Greetings
#2

[eluser]Unknown[/eluser]
After some research in the form_validation.php library i came to the conclusion, that this could really be a bug.

For me it worked after replacing the recursive call at the top of _execute by a new one:
Code:
function _execute($row, $rules, $postdata = NULL, $cycles = 0)
    {
        // If the $_POST data is an array we will run a recursive call
        if (is_array($postdata))
        {
            foreach ($postdata as $key => $val)
            {
                //$this->_execute($row, $rules, $val, $cycles); // THIS IS WHAT CI DOES USUALLY
                $this->_execute($row, $rules, $val, $key); // THIS IS WHAT SOLVED MY PROBLEM
                $cycles++;
            }

            return;
        }
// ... and so on

$cycles is later used as the array index of the posted array. but it always starts to count at 0, so if we have unordered array indices (which is in fact possible by giving the form element a concrete index in the name field), it would not work.

BUT: i did not test this for other solutions than mine, for example multidimensional arrays
#3

[eluser]NeoArc[/eluser]
Hmm, is the $cycles parameter really needed? (Sorry for the bump)

Perhaps it should be renamed instead.




Theme © iAndrew 2016 - Forum software by © MyBB