CodeIgniter Forums
isset/required validation bug - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: isset/required validation bug (/thread-2730.html)



isset/required validation bug - El Forum - 08-21-2007

[eluser]mjijackson[/eluser]
This bug is discussed on this thread. In that thread, some users found a clever workaround for what is actually a bug in the code. When validation runs, it short-circuits any fields that have an 'isset' or 'required' rule if they are in fact not set. However, in the short-circuit, it neglects to set the error message for that field. This can be remedied with the following:

Code:
$error = $field . '_error';
$this->$error = $message;

It's keeping the code behavior consistent throughout the Validation class. The code should be changed around line 232 of Validation.php in order to squash the bug.


isset/required validation bug - El Forum - 08-21-2007

[eluser]Derek Allard[/eluser]
Thanks mjijackson. Could you post the full function that you are proposing?


isset/required validation bug - El Forum - 08-22-2007

[eluser]mathgl67[/eluser]
i have a litle patch for this issue. i don't know if it's fixing the whole problem.

Code:
diff -r 680e129277f9 -r 114fcf6410d5 system/libraries/Validation.php
--- a/system/libraries/Validation.php    Wed Aug 22 15:27:55 2007 +0200
+++ b/system/libraries/Validation.php    Wed Aug 22 15:33:28 2007 +0200
@@ -227,9 +227,13 @@ class CI_Validation {
                     {
                         $line = $this->_error_messages['isset'];
                     }
-                    
+
+                    // Set errors variables.
+                    $error = $field.'_error';
                     $field = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
-                    $this->_error_array[] = sprintf($line, $field);    
+                    $message = sprintf($line, $field);
+                    $this->_error_array[] = $message;
+                    $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
                 }
                        
                 continue;
@@ -732,4 +736,4 @@ class CI_Validation {

}
// END Validation Class
-?>
\ No newline at end of file
+?>



isset/required validation bug - El Forum - 08-22-2007

[eluser]mjijackson[/eluser]
The full function is too large to post in this forum. Instead, I'll post the relevant portion. This is starting on line 206 of system/libraries/Validation.php:

Code:
/*
* Are we dealing with an "isset" rule?
*
* Before going further, we'll see if one of the rules
* is to check whether the item is set (typically this
* applies only to checkboxes).  If so, we'll
* test for it here since there's not reason to go
* further
*/
if ( ! isset($_POST[$field]))
{            
    if (in_array('isset', $ex, TRUE) OR in_array('required', $ex))
    {
        if ( ! isset($this->_error_messages['isset']))
        {
            if (FALSE === ($line = $this->CI->lang->line('isset')))
            {
                $line = 'The field was not set';
            }
        }
        else
        {
            $line = $this->_error_messages['isset'];
        }
        
        // Build the error message
        $field = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
        $message = sprintf($line, $field);
        
        // Set the error variable.  Example: $this->username_error
        $error = $field.'_error';
        $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
        
        // Add the error to the error array
        $this->_error_array[] = $message;
    }
    
    continue;
}

The above post is also correct.


isset/required validation bug - El Forum - 08-22-2007

[eluser]mathgl67[/eluser]
I think the error variable should be set before building the error message. Else the error variable will be set with the text define in set_fields().

Code:
// Set the error variable name.  Example: $this->username_error
        $error = $field.'_error';
        
        // Build the error message
        $field = ( ! isset($this->_fields[$field])) ? $field : $this->_fields[$field];
        $message = sprintf($line, $field);
        
        // Set the error variable.
        $this->$error = $this->_error_prefix.$message.$this->_error_suffix;
        
        // Add the error to the error array
        $this->_error_array[] = $message;



isset/required validation bug - El Forum - 08-22-2007

[eluser]mjijackson[/eluser]
The error message is supposed to contain the text defined in set fields. That's one of the reasons why that function exists. Look further down in the code and you'll see that the same thing happens a few lines further down.


isset/required validation bug - El Forum - 08-29-2007

[eluser]mathgl67[/eluser]
UP ! Issue need more information to be fixed ?