Welcome Guest, Not a member yet? Register   Sign In
Bug in validation library
#5

[eluser]Donny Kurnia[/eluser]
Derek, today I'm revisit the Validation library.

After I change
Code:
if ($this->_safe_form_data == FALSE OR $data == '')
to become
Code:
if ($this->_safe_form_data == TRUE OR $data == '')
, all of validation value whether it have prep_for_form rule or not will be prepped.
So I start to dig the library to find out how prep_for_form work.
I stumble upon this code:
Code:
function set_fields($data = '', $field = '')
    {    
        if ($data == '')
        {
            if (count($this->_fields) == 0)
            {
                return FALSE;
            }
        }
        else
        {
            if ( ! is_array($data))
            {
                $data = array($data => $field);
            }
            
            if (count($data) > 0)
            {
                $this->_fields = $data;
            }
        }        
            
        foreach($this->_fields as $key => $val)
        {
            $this->$key = ( ! isset($_POST[$key])) ? '' : $this->prep_for_form($_POST[$key]);
            
            $error = $key.'_error';
            if ( ! isset($this->$error))
            {
                $this->$error = '';
            }
        }        
    }

and this one in run() function

Code:
if ($total_errors > 0)
        {
            $this->_safe_form_data = TRUE;
        }
        
        $this->set_fields();
I tried to understand it, so correct me if I'm wrong.
1. When I call $this->validation->run() and there is error in the form ($total_error > 0), the private variable $_safe_form_data will be set to TRUE, otherwise it will be FALSE as it's initial value.
2. Function set_fields() will be called. In here, function prep_for_form will be called if $_POST[$key] is isset.
3. In prep_for_form() original code:
Code:
function prep_for_form($data = '')
    {
        if (is_array($data))
        {
            foreach ($data as $key => $val)
            {
                $data[$key] = $this->prep_for_form($val);
            }
            
            return $data;
        }
        
        if ($this->_safe_form_data == FALSE OR $data == '')
        {
            return $data;
        }

        return str_replace(array("'", '"', '<', '>'), array("'", "&quot;", '&lt;', '&gt;'), stripslashes($data));
    }
the $this->_safe_form_data will be checked if it FALSE or not. So, the prep_for_form() will only take effect if $this->_safe_form_data is TRUE and $data != '', in this case it mean that there is error occured when validation run() function is called.

This mean that whatever the validation rule is, if the validation run() result is error free, prep_for_form will never take effect. If there is error, prep_for_form will always run() even there is no prep_for_form rule in it.

So I'm suggestion that there should be another function for automatic prep_for_form that will be called when the validation have error, and prep_for_form for the validation rule. Or it should never be called automatically when the error occured.

In my application, I use prep_for_form in rules and expect it run only in the field that have rules with it, and never run automatically when the form have error in validation.


Messages In This Thread
Bug in validation library - by El Forum - 08-10-2008, 10:34 PM
Bug in validation library - by El Forum - 08-11-2008, 05:23 AM
Bug in validation library - by El Forum - 08-11-2008, 07:40 AM
Bug in validation library - by El Forum - 08-11-2008, 11:01 PM
Bug in validation library - by El Forum - 08-24-2008, 11:18 PM
Bug in validation library - by El Forum - 09-24-2008, 04:58 PM
Bug in validation library - by El Forum - 09-24-2008, 07:54 PM



Theme © iAndrew 2016 - Forum software by © MyBB