Welcome Guest, Not a member yet? Register   Sign In
Validating array of inputs (or other elements) with non int keys
#1

[eluser]Krzemo[/eluser]
Cheers,

So I run into this thing today:
Im submiting a form with array of inputs like:

<input type="text" value="" maxlength="50" id="fr" class="txt valid" name="cityname[fr]">
<input type="text" value="" maxlength="50" id="en" class="txt valid" name="cityname[en]">
<input type="text" value="" maxlength="50" id="de" class="txt valid" name="cityname[de]">

and then I try to validate them with form validation lib. And guess what... whatever im putting as rule its always true.
I spent whole day finding out why and it appears that CI FV lib doesnt like arrays with keys set to something other then INTs.

Im to tired to be sure and to judge if thats on purpose or something is missing, so I thought I would let you know and ask other ppl opinion.

I alredy did a tweak to _execute function of FV lib which is changing lines 544 to 555:
Code:
if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
            {
                // We shouldn't need this safety, but just in case there isn't an array index
                // associated with this cycle we'll bail out
                if ( ! isset($this->_field_data[$row['field']]['postdata'][$cycles]))
                {
                    continue;
                }
            
                $postdata = $this->_field_data[$row['field']]['postdata'][$cycles];
                $_in_array = TRUE;
            }
to this:
Code:
if ($row['is_array'] == TRUE AND is_array($this->_field_data[$row['field']]['postdata']))
            {
                $tmparr = array();
                $tmpcnt = 0;

                foreach($this->_field_data[$row['field']]['postdata'] as $key => $val)
                {
                    $tmp[$tmpcnt] = $key;
                    $tmpcnt++;
                }
                // We shouldn't need this safety, but just in case there isn't an array index
                // associated with this cycle we'll bail out
                if ( ! isset($this->_field_data[$row['field']]['postdata'][$tmp[$cycles]]))
                {
                    continue;
                }
            
                $postdata = $this->_field_data[$row['field']]['postdata'][$tmp[$cycles]];
                $_in_array = TRUE;
            }
but I havnt tested it yet and becouse of being tired cant think of any possible flaws.

Anyway, I'd appreciate if someone could tell me if its just me being paranoid or to picky (or having wrong approach with those keys) or that is something to be considered in future releases.

Regards
K.


PS
I just changed my mind and I'll be using INTs as keys anyway so this is just theoretical subjet.
#2

[eluser]Krzemo[/eluser]
Short update:
IMHO none of the elements arrays with keys either they are string or ints are validated properly...




Theme © iAndrew 2016 - Forum software by © MyBB