Welcome Guest, Not a member yet? Register   Sign In
Form_validation prob when validating array form fields
#1

[eluser]minerbog[/eluser]
Hi All,

Had a problem with the form validation when trying to validate a form field that was part of an array.

When CI sensed it was an array it would put in through the _reduce_array function. If the field was blank it would return a empty string. This was a problem when it came to preg_matching as preg_match will return false on an empty string and the section of code that catches empty fields only catches NULL values, so it was returning a validation error on an empty field!!

Below is my mod (although crud), that gets round this problem. May be its something for v2 of CI...

Code:
/**
* Traverse a multidimensional $_POST array index until the data is found
*
* @access    private
* @param    array
* @param    array
* @param    integer
* @return    mixed
*/        
function _reduce_array($array, $keys, $i = 0)
{
    if (is_array($array))
    {
        if (isset($keys[$i]))
        {
            if (isset($array[$keys[$i]]))
            {
                $array = $this->_reduce_array($array[$keys[$i]], $keys, ($i+1));
            }
            else
            {
                return NULL;
            }
        }
        else
        {
            return $array;
        }
    }
    //modified to return NULL instead of an empty string
    if ($array=='')
    {
        unset($array);
        $array = NULL;
    }
    return $array;
}




Theme © iAndrew 2016 - Forum software by © MyBB