CodeIgniter Forums
Form_validation run() method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Form_validation run() method (/showthread.php?tid=61326)



Form_validation run() method - richard.rhsr - 04-08-2015

Greetings!

I've been reading the Form_validation implementation,
and wondering why does the run() method return FALSE if there is not any rule set.

Wouldn't the opposite (return TRUE unless some rule is not attended) be more intuitive?


RE: Form_validation run() method - RobertSF - 04-08-2015

Well, I guess it could go either way. It seems to me that one reason the run() method returns FALSE when there are no validation rules is that the method in fact hasn't validated anything. Comments in the code would seem to support my theory (or wild guess).

PHP Code:
public function run($group '')
{
    
// Do we even have any data to process?  Mm?
    
if (count($_POST) == 0)
    {
        return 
FALSE;
    }

    
// Does the _field_data array containing the validation rules exist?
    // If not, we look to see if they were assigned via a config file
    
if (count($this->_field_data) == 0)
    {
        
// No validation rules?  We're done...
        
if (count($this->_config_rules) == 0)
        {
            return 
FALSE;
        } 

If you have a form you wish to process without any validation rules, you can still use the alternate style of checking for submit and then taking the values from the $_POST array.

PHP Code:
if ($_SERVER['REQUEST_METHOD'] == 'POST'
{
 
   // form was submitted
 
   // process the data, if any
}
else
{
 
   // perhaps redisplay form




RE: Form_validation run() method - alkarim - 04-22-2015

Well said man....didn't thought that way Smile