Welcome Guest, Not a member yet? Register   Sign In
find if a rule group exists (when set as $config in form_validation.php)
#1

[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
#2

[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...
    }
}
#3

[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?
#4

[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




Theme © iAndrew 2016 - Forum software by © MyBB