• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solved: Form Validation Clarification: Configuration Array w/ Sets of Rules

#1
[eluser]w3ttr3y[/eluser]
I apologize as this is a rather basic question, but I've been struggling with it for awhile. I had form validation working, but as I read more I really liked the idea of moving validation to the configuration file.

First of all, let me make sure my long term goal is possible. Can I use the groups with an autoloaded configuration file? (I wouldn't see why not, but as I'm having problems I figured I should ask)

Since that was failing and some log messages I added indicated it was due to a lack of rules, I decided to try explicitly passing the rules array to the validation library.

If I explicitly set the rules, and then do an empty run it works as expected.
Code:
$this->form_validation->set_rules($rules['register']);
        if($this->form_validation->run() === FALSE ) {...

According to the user guide, I can pass the name to the run method, so I thought I should be able to do the following...
Code:
$this->form_validation->set_rules($rules);
        if($this->form_validation->run("register") === FALSE ) {...

I can't get that to work, however. Is there a difference in the configuration array between the two methods that I didn't pick up on?

I really appreciate any guidance!

Edit October 8, 2010: After looking at the code, it looks like if you set rules with the set_rules function you can't use groups. That's fine, I was just trying trouble shoot why the rules weren't automatically loading so I'll just revert to my original problem.

Second edit October 8, 2010: After debugging some more, I've figured out that in that the config file is loaded but that the $this->_config_rules variable never gets set so it never finds the rules.

Edit October 10, 2010: Solved. I was sub-classing Form_Validation (i.e. My_Form_validation) and in the constructor I wasn't accepting a rules argument. So of course it worked initially (with setrules) but not when I passed them through the constructors. For some reason, I needed to add the rules parameter in order to get the rules from the config file to work (I haven't dug into why, but when I fixed it they started working.

Thanks,
Bill

#2
[eluser]InsiteFX[/eluser]
If you place your rules in application/config/form_validation.php
it will be autoloaded by CodeIgniter.

InsiteFX

#3
[eluser]w3ttr3y[/eluser]
I may not have had the underscare in the file name, I will double check tonight. Thanks for the tip.

Even with the config file, I'll need to use groups, correct? Otherwise it won't know what rules to apply (unless I do the Controller/Action method, but I prefer to try and keep validation controller independant so I can reuse it and restructure the site). So before I can really test, don't I need to figure out what the two example aren't identical in practice?

Thanks,
Bill

Edit: I dobule checked and I did have the underscore in there. Based on the logs, I dont' have any rules so validation is automatically failing. Since its also failing when I try to manually pass the data, I'm assuming I have a problem with my rules. My hope is that if I get it so I can manually pass the rules in and it works then I'll be one step closer to automatically having the rules automatically be applied.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.