CodeIgniter Forums
validation prep_for_form array data bug - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: validation prep_for_form array data bug (/showthread.php?tid=6266)



validation prep_for_form array data bug - El Forum - 02-20-2008

[eluser]Wilker[/eluser]
When you post some array data in post (like multiple checkbox) script generate warning because the function tries use str_replace in one array. the real bug is only in one line that is not where he was to be, this is the actually prep_for_form function:

Code:
function prep_for_form($data = '')
{
    if (is_array($data))
    {
        foreach ($data as $key => $val)
        {
            $data[$key] = $this->prep_for_form($val);
        }
    }
    
    if ($this->_safe_form_data == FALSE OR $data == '')
    {
        return $data;
    }

    return str_replace(array("'", '"', '<', '>'), array("'", "&quot;", '&lt;', '&gt;'), stripslashes($data));
}

for fix this problem, just add a line after foreach inside is_array if, like this:

Code:
function prep_for_form($data = '')
{
    if (is_array($data))
    {
        foreach ($data as $key => $val)
        {
            $data[$key] = $this->prep_for_form($val);
        }
        
        return $data; //line added
    }
    
    if ($this->_safe_form_data == FALSE OR $data == '')
    {
        return $data;
    }

    return str_replace(array("'", '"', '<', '>'), array("'", "&quot;", '&lt;', '&gt;'), stripslashes($data));
}

this makes the function returns array data prepared, and the script don't reach str_replace this way.