CI form 'best practices' |
[eluser]Maglok[/eluser]
So when we make a form within a CI application we have several options. To use the Form Validation library, and the form helper. I have been working with the helper for a while, but to me it generates a lot of code and not that much effect. It is very easy to populate a dropdown menu for instance. Doing it through basic HTML will create some serious code. It seems to mess up my MVC when I use it though. I get pieces of code like this: Code: $data['aantallen_settings'] = array( In the Controller. And the size and class of a input field is kind of a View thing. What is the best practice there? I have heard a bunch of CI veterans say 'dont use the form helper to begin with', true? Then take form validation. Should I put a rule on each field just so I can repopulate it? Is that the best practice? Sounds overly complicated for fields that are not interesting to check, like text areas in which I allow HTML. On that subject: Is there no extended base of custom made validations, like date, out there?
[eluser]Chicken's Egg[/eluser]
I Don't understand your first question, so I will start with the second one. I don't see any reason to avoid the form helper. Yes, creating forms in html isn't that much work for an experienced html-writer, but the form_validation library works well together with form helper. As for your third question. Yes, you should put a rule (remember: empty rules are rules too) on each field to get it repopulated when the form validates to false. For textareas you might consider using xss_clean and trim. If you would like to validate file uploads too, you might also be interested in this extension of the library: http://ellislab.com/forums/viewthread/123651/ But there are others (and more extensive ones) out there. However, if you don't feel the need for an extension, you could use a callback to check the validity of a date.
[eluser]Chad Fulton[/eluser]
Here's a simple, possible solution: Controller Code: // ... display_forms View Code: // ...
[eluser]Maglok[/eluser]
First question clarification: MVC dictates we handle any view related stuff in the view. Stuff like the color of something, the class (for CSS), etc. Yet the array of attributes we give a field in the Controller includes stuff that is View related. @Chicken's egg: Yet the validation class user_guide uses normal html to show you how to do it. You got a point there though. Putting an empty rule on a field kind of seems redundant to me. Sure I can do it, but it's a tad bit strange. @chad Fulton: Yeah possible, but I wonder if there is not an easier way. Is that really best practice? To split it and then merge it in the view? It sounds a bit like a long workaround that only adds even more code and complexity.
[eluser]skunkbad[/eluser]
You make it sounds like you're in an MVC prison! I'd hate to show you some of my code. I think there are ideals, and I try to have my forms in views. I try not to use the form helper, but I do use the form validation class. Form validation obviously goes in a controller. I have some language files that hold form related error messages and confirmation messages. To me (because I was coding purely procedural until about 6 months ago) I see there being a point where there are just too many files to make something simple happen.
[eluser]Yash[/eluser]
Dude the best practice with CI is more you code more you get new way to code .... so chill enjoy CI
[eluser]Maglok[/eluser]
Haha I am not in a 'prison' I just like to have my code clear and readable. The application expands rapidly and anything to make it clearer, MVC helps with that o course.
[eluser]bretticus[/eluser]
Well, I'm a relatively new CI user. Perhaps I was used to doing code similar to Derek, but I have had no qualms using form helper within my views and particularity with dynamically-populated input/controls. I too think that having to make blank rules for inputs that I do not need to validate seems like bloat but considering some of my form validation classes from the past, I'm actually a little bit lighter with CI. I NEVER have html meta data in my controller that gets passed to my view (I'd be interested to see a case for that.) And if I have a commonly used validation, I ALWAYS extend the validation class as to include it in my pipe lists of rules. Here's a recent example: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
Welcome Guest, Not a member yet? Register Sign In |