![]() |
Form Validation - Best Practice - 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: Form Validation - Best Practice (/showthread.php?tid=50060) |
Form Validation - Best Practice - El Forum - 03-12-2012 [eluser]lachavvy[/eluser] Hi Guys, Sorry if this has already been debated before, but I'm in the middle of writing some form validation for my website (www.premieradverts.com) and I have found writing all my form validation in the controller to be too much code so I've put it in a library called validate. Is this good practice or is this something that shouldn't be done as it is only really going to be used for one controller? Code: <?php This has allowed me to call from my controller Code: $this->validate->new_owner(); Rather than the whole block of validation code. It's easier for me to read, but is it good practice? Any help would be greatly appreciated. Form Validation - Best Practice - El Forum - 03-12-2012 [eluser]CroNiX[/eluser] You can also put your rules in a config file. http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#savingtoconfig Form Validation - Best Practice - El Forum - 03-12-2012 [eluser]Aken[/eluser] I do all my form validation logic in the controller. You can do it that way if you prefer, there really isn't a specific way you need to do it. As long as you don't add a ton of extra steps when you don't have to (which you aren't). Form Validation - Best Practice - El Forum - 03-12-2012 [eluser]jellysandwich[/eluser] Models are typically the best place to put validation, especially for reusability. No need to create a separate library imo, and you can even use the same syntax as in controllers ($this->form_validation vs $CI->form_validation). Form Validation - Best Practice - El Forum - 03-13-2012 [eluser]lachavvy[/eluser] Thanks for the quick replies. I didn't realise there was a function already within form_validation, which I should have looked at originally. It seems useful, though having to create an array for each rule seems a bit excessive. I'll continue developing my site using the library but will look into the other methods once I start a new site. Cheers again ![]() Form Validation - Best Practice - El Forum - 03-13-2012 [eluser]Mauricio de Abreu Antunes[/eluser] Take a look at my library. Simple but works fine: Code: class MY_Form_validation extends CI_Form_validation { This way, you just need to call $this->form_validation->valida_dados('your_grou_name', 'your_delimiter'). |