![]() |
form rules/values: helper, controller or model? - 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 rules/values: helper, controller or model? (/showthread.php?tid=33513) |
form rules/values: helper, controller or model? - El Forum - 08-29-2010 [eluser]domsworkshop[/eluser] Howdy folks, I've been developing with CodeIgniter for a few months and developed my site using this wonderful framework, and I know just enough PHP/MySQL to be dangerous. Now that I've gained a certain comfort level with the framework I'm debugging and improving code, while also trying to adapt each structure (ie user structure, blog structure etc) to be more modular, allowing me to offer sets of files for download so that other beginning developers can benefit from some of the things I've figured out. One of these is a very basic helper that stores arrays of form fields and form rules, with qualifier loops, for the different structures. Currently everything is stored all within the same helper - comments, users, invoices, etc. but I'd like to break it apart (in the interests of above goals and also to make each structure more lightweight). My question is this: which of the following options would be considered best practice? - separate each set of rules into its own helper; this seems the best bet but would mean a lot of helpers; is that bad? - rewrite the arrays as private functions within the controller; this sounds nice, but complicates matters if I would like other controllers to be able to use these values (or does it?) - place the arrays as private functions within the model; this seems feasible because the fields for each structure are ultimately used by the corresponding model, and could be called by different controllers, but the functions themselves wouldn't actually interact with the database. I like this idea best but don't know if it's a bad practice to have functions like that cluttering up my models. Thanks for any advice you can offer! form rules/values: helper, controller or model? - El Forum - 08-29-2010 [eluser]domsworkshop[/eluser] Forgot to include a code example. Here's what I have in my current values helper. Code: if (!function_exists('form_rules')) { form rules/values: helper, controller or model? - El Forum - 08-29-2010 [eluser]Vinzent Zeppelin[/eluser] I think validation rules would be best placed along with the data to which they apply, in the corresponding models. Take a look at the DMZ library; it includes validation rules and relations in private attributes, and automatically validates data when it is entered. form rules/values: helper, controller or model? - El Forum - 08-29-2010 [eluser]domsworkshop[/eluser] I'm glad you agree...I just tried integrating the user rules and field values into my user model and it looks like much cleaner code and much easier for data prep. Thanks! form rules/values: helper, controller or model? - El Forum - 08-29-2010 [eluser]Vega[/eluser] Check out: saving sets of rules to a config file You can then run the validation rules above like: Code: if ($this->form_validation->run('post') !== TRUE) Personally I don't like putting validation or anything else in the model or else it doesn't really abide by the MVC principal. It should be database work only. form rules/values: helper, controller or model? - El Forum - 08-29-2010 [eluser]domsworkshop[/eluser] Initially when I wrote this helper I was looking at that document, but it really only seemed viable for validation rules (at least for my current proficiency level); I also want a DRY solution for outputting form fields, and database qualifiers to prevent unwanted SQL insertion, and it seems that grouping three utility functions for each of these things together in one file (whether helper, controller, model, etc.) makes things a little bit more neat and tidy without code scattered across too many files. |