CodeIgniter Forums
find if a rule group exists (when set as $config in form_validation.php) - 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: find if a rule group exists (when set as $config in form_validation.php) (/showthread.php?tid=20978)



find if a rule group exists (when set as $config in form_validation.php) - El Forum - 07-27-2009

[eluser]jpschroeder[/eluser]
I dynamically process forms that are submitted. This means that I may submit a form that doesn't have any validation written for it yet, but I will still try to load a rule group. For example:

Code:
function handle_form($table){
    if($this->form_validation->run($table) == false){
        //validation failed so do these things...
            
        // check to see if there was a rule group for this table...
        if($this->config->item($table)){  // <--- the line I don't know the proper code for...
              echo validation_errors();
        }else{
              echo "no form validation set yet, please make one before continuing";
        }

    }else{


          //validation passed, handle the data...
    }
}

config/form_validation.php:

Code:
$config = array(
                    'users' => array(
                                            array(
                                                "field" => "first_name",
                                                "label" => "First Name",
                                                "rules" => "required"
                                                ),
                                            array(
                                                "field" => "last_name",
                                                "label" => "Last Name",
                                                "rules" => "required"
                                                )
                                        )
);


Now the validation works just fine if there is a rule set for the $table. But if there isn't a rule group set, the validation doesn't pass, which is good, but I want to know that it failed because there wasn't a rule group set. In order to do that I have to be able to somehow check to see if that rule group exists. Can someone please let me know to access the $config variable set in config/form_validation.php? I thought this would work:

Code:
if($this->config->item('rule_group'))

but it just returns false every time even when the group exists. Thanks in advance!




Thanks in advance


find if a rule group exists (when set as $config in form_validation.php) - El Forum - 07-27-2009

[eluser]davidbehler[/eluser]
The form_validation.php file is not loaded as a config file like config.php but is rather used to pass a parameter ($config in this case) to the form validation library.

What you might do is this:
Code:
function handle_form($table){
    if($this->form_validation->run($table) == false){
        //validation failed so do these things...
            
        include(APPPATH.'config/form_validation.php');
        if(isset($config[$table])){
              echo validation_errors();
        }else{
              echo "no form validation set yet, please make one before continuing";
        }

    }else{


          //validation passed, handle the data...
    }
}



find if a rule group exists (when set as $config in form_validation.php) - El Forum - 07-27-2009

[eluser]jpschroeder[/eluser]
Thanks for the fast reply waldmeister. That code works great.

Would that be the preferred method of accessing that data in CI...there's no spiffy function written to do that huh?


find if a rule group exists (when set as $config in form_validation.php) - El Forum - 07-27-2009

[eluser]davidbehler[/eluser]
There is no such method, no.
Till now I've never seen a person who needs such a feature.

You might add the form_validation.php to the config autoload array, but that could cause some problems. E.g. if you have a config item with the name of a table for which you have a validation rule aswell, the later might override the first.

But if you are sure, that there will be no such problem, then you can autoload the form_validation config file and access the validation rule like you proposed in your first post:
Code:
$this->config->item($table);

There just isn't always a "CI way" to do something Wink