Welcome Guest, Not a member yet? Register   Sign In
Form Validation config file confusion
#1

[eluser]silent (e)[/eluser]
I'm very confused about when the application/config/form_validation.php file gets loaded?

According to the Form Validation class documentation, in bold green text no less, the file is loaded automatically when you call the run() function. Looking at the code where the run function is defined, however, shows that's not true and it's not loaded when the Form Validation library is loaded either.

Searching the forums has shown that people have solved this by adding it to the config section of autoload.php or by CI->load->config('form_validation). That does work, however it introduces a potential problem, imho. When loading the form_validation config file, it pollutes the top-level config.

If you use the method of naming your validation array sets (not the controller/action naming setup) then each of the names becomes a top-level config key. If you name one of your sets "language" then that config key is overwritten with your validation rules. Bad bad bad.

So, there are several issues raised here.

1) the fact that the documentation is wrong and that the form_validation.php config file is NOT loaded during run() and ...
2) autoloading the form_validation.php file can cause config data collision.

Thoughts? I have some on fixes but not sure if they'd work without breaking backward compatibility.

matte
#2

[eluser]Ivar89[/eluser]
Form vaidation is a librarie so you have to load it to use it.
Code:
$autoload['libraries'] = array('form_validation', 'session');
Just like when you are using the session librarie.
When this is done thr documantation is in fact correct.

I don't know about the collision thingy^^
#3

[eluser]silent (e)[/eluser]
Clarification: I'm loading the library and config outside the autoload.php file, directly in the controller when I need it since they aren't needed across the entire site.

The documentation is still wrong. The validation rule file is NOT loaded "automatically" when you call the run() function. And nowhere on that page does it suggest that you need to add it to the autoload.php file or dynamically load it (as I am). A beginner, as I am, to using config rules instead of just multiple set_rules() calls will get confused.

That block of documentation, when I read it for the very first time, implied that there was code that checked for the existence of the form_validation.php config file and if it existed then it would load it. It would be better if they explained that you must either manually load the file or add it to the autoload config.

So, I do understand when the file gets loaded, despite the problem with the docs not explaining it well enough... There's still the problem with top-level config namespace collision.

BTW it gets even worse if you aren't naming your validation config arrays. Now, you have generic numeric keys in the config array.

See this gist for what the global CI config array looks like with form validation rules loaded. Both a named set and a global set
#4

[eluser]tomcode[/eluser]
1. The documentation states
Quote:Your validation rule file will be loaded automatically and used when you call the run() function.


2. As with any other library: on loading the library CodeIgniter looks for a config file with the same name (lower case), reads it and passes the found $config variable as param to the constructor.
#5

[eluser]silent (e)[/eluser]
ooh. now THERE's the information I needed. Thanx tomcode. I'll be investigating that. It's still, imho, incorrectly worded. It should probably read "Your validation rules will be loaded automatically...". Not that the rule file will be loaded when you call run(). Because, as you just mentioned, the rule file is loaded when the library is loaded, not during the run() function.

Sorry to sound like a broken record but is no one else seeing the potential problems with the config namespace collisions? I'll gladly scurry back into my burrow if everyone else thinks I'm being over-paranoid. My quick idea was to have CI load the config settings into a key named for the library but that's without any real research.
#6

[eluser]tomcode[/eluser]
For me it's correctly worded.

name space problem: have never had a problem with.
#7

[eluser]JuanG[/eluser]
Hello, I totally agree with silent (e), the documentation is confusing at first, and the fact that I came to the forum to find a solution is the proof of it. i didn't need to do that before because everything else I've come across is pretty clear.

Thanks to your posts here I understood the problem but I'd advise reconsider changing that small piece of text to improve clarity.

Ci is a great framework, I'm enjoying using it and it documentation, forum discussions and user's feedback is amazing.

Cheers,
#8

[eluser]JuanG[/eluser]
By the way guys, since I moved my validation rules to a form_validation.php file, I've gotten this php notice in my view:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: config

Filename: controllers/admin.php

Line Number: 29

I've been looking all over and I can't find the proper way to deal with this notice. In regular php programming I'll use the isset($config) or something like that to avoid those notice messages.

Can you help me treat this case correctly inside CI?

Thanks in advanced for your help.

Cheers,
#9

[eluser]Ivar89[/eluser]
You probably loaded $config which is automaticly loaded in CI so maybe thats it.
What does your form validation look like?
I also use form_validation.php and works fine with me
this is what I use:
Code:
$config = array('contact' => array(array('field' => 'Field name',
                         'label' => 'label name',
                     'rules' => 'trim|required|callback_something'
                    ),));
//and some more fields.

controller:
Code:
if ($this->form_validation->run('contact') == true)
{
$Do some stuff
}
else
{
Don't do some stuff/
}
#10

[eluser]JuanG[/eluser]
Well I kind of solved it like I told you guys on the post above: set_rules(isset($config)), but of course I still don't know if that's the best way to deal with it.

My form_validation.php looks like this:

Code:
$config = array(
               array(
                     'field'   => 'uname',
                     'label'   => 'Username',
                     'rules'   => 'trim|required|min_length[5]|max_length[20]|xss_clean'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'trim|required|matches[confpassword]|md5|min_length[6]'
                  ),
               array(
                     'field'   => 'confpassword',
                     'label'   => 'Password Confirmation',
                     'rules'   => 'trim|required|min_length[6]'
                  ),  
               array(
                     'field'   => 'fname',
                     'label'   => 'Full Name',
                     'rules'   => 'trim|required|max_length[50]'
                  ),
               array(
                     'field'   => 'email',
                     'label'   => 'Email Address',
                     'rules'   => 'trim|required|valid_email|max_length[50]'
                  ),
               array(
                     'field'   => 'queue',
                     'label'   => 'Cluster Queue',
                     'rules'   => 'trim|required'
                  )
            );

and my controller looks like this:

Code:
function insert_user()
    {
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        $this->form_validation->set_rules(isset($config));
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('add_view');
        }
        else
        {
            $data = array(
                        'uname'      => $this->input->post('uname'),
                        'password'   => $this->input->post('password'),
                        'fname'      => $this->input->post('fname'),
                        'email'      => $this->input->post('email'),
                        'queue'      => $this->input->post('queue'),
                    );
            
            $this->db->insert('users',$data);
            redirect('admin');
        }
    }

as you can see, I'm using "set_rules(isset($config))" and the "isset()" helps me get rid of the Notice but, do you see something else that could be done here? or something wrong?

I'm just a newbie, this is my first approach to a form with CI. I'm pretty happy with the framework though.

Thanks again for your help,

Cheers.




Theme © iAndrew 2016 - Forum software by © MyBB