best practices on Form Reuse - keeping all forms in a central location with validation |
[eluser]chuckleberry13[/eluser]
No I wasn't implying you or anybody should have two separate Controllers processing the same form. I was asking if we agree that the Controller should handle the validation rules then how to share that form across several Controllers without having to re-write the validation rules every time I want to include that particular form.
[eluser]Randy Casburn[/eluser]
I'm with you and your way cool handle chuckle! a form is a form is a form...it is not locked to a controller or a view or an application or a panel or the left side or middle or top of a page. It may be used in twelve different applications that are similar but with different controllers and views and databases...but the forms might just have the same field sets (or similar). I'm with you on this. Randy p.s. it is true that I'm a little nuts though.
[eluser]chuckleberry13[/eluser]
@randy your killing me with this going insane thing. The more I think about your idea the more it makes sense. That way if I include the same field but in another form its validation rules/callback function is triggered. I am thinking I can just set a bank of validation rules and test if that field was passed in and if so attach its validation rule to the $rule array. I like the idea of that. I'm going to play around with it and see what becomes of it. Thanks for your help. @Colin I am still interested in alternative methods if you would care to elaborate.
[eluser]Colin Williams[/eluser]
Quote:That way if I include the same field but in another form its validation rules/callback function is triggered. I am thinking I can just set a bank of validation rules and test if that field was passed in and if so attach its validation rule to the $rule array. Not a bad idea. You're essentially building a system of form widgets. I think a well thought-out form widget library could be in order for that. The only concern is that it would lock you into strict conventions (although convention over configuration is all the rage, if the convention doesn't extend all the way through the process, your just creating more work somewhere else). Rules are already fairly simple to define, but it's that extra processing that current rule functions can't do by themselves that might be useful. Quote:a form is a form is a form...it is not locked to a controller or a view or an application or a panel or the left side or middle or top of a page I don't think I was speaking to the contrary. But I'm not sure I can imagine a single form shared among Controllers. Even if it had the exact same field names and HTML markup, it's going to serve an individual purpose. And what happens when one Controller requires more from the form? You've got to go ahead and make yourself two separate forms anyway. Feel free to prove me wrong by example.
[eluser]chuckleberry13[/eluser]
The situation I'm running into is my user sign up form. I'm finding I need to display that form in several locations because of the nature of the project. Just trying to find the best way to do that and so far wigetizing the forms seems to be the only way I can come up with. I am open to another way.
[eluser]JoostV[/eluser]
Since setting validation, friendly usernames, etc is a repetitive taks, you can put these tasks in a custom library, forms.php. This will do all validation setup, validation itself, and return prepped input to the controller. The only 'flaw' is that my forms.php spits out html for every input (not the entire form, but just the <input type="text" name="foo" value="bar" /> ![]() In your controller, you simply set up an array containg some metadata about the form and all the fieds that should go in your form: Code: // Form metadata Now, from your controller, you call the libary and pass your form array: Code: // Load form library The libary will return an object to the controller, holding: 1. form metadata, eg: Code: $form->metadata['validated'] = false; Code: $formelements[0] = '<input type="text" name ="lg" maxlength="16">'; Code: $form->form_values['lg'] = 'Jopie'; Next thing you do is out all the form logic in the library. It's a tedious task, but you will only need to do it once and it will leave you with a generic, reusable library. To the library, an array of form elments is passed. This array contains info about name, attributes, type, user friendly name, validation an initial value for every field. In the library forms.php, you write functions that 1. set validation, like Code: // Construct validation array 2. Set friendly user names: Code: // Construct an array of friendly user names 3. Run validation Code: if ($CI->validation->run() == true) { // Validation was successful 4. For every type of input you need to write a function that will return the html for the field. This is an example: Code: function input($element) Summarizing, when you have initiated you form in the controller, you will have an object that contains: 1. metadata about the id of the form, wheteher it was posted, whether it validated or not, etc. 2. html for every input in the form 3. a label for every input in the form 4. validation error string 5. values that you can store to a database, etc if the form was succesfully validated.
[eluser]chuckleberry13[/eluser]
Thanks for sharing that JoostV. I might look into it sometime but last night I setup the controller handling the form to take in an extra argument which will make it a view partial and I think I like that setup the best for now and here is my reason. I can easily include that form anywhere with one line of code in any of my views like this. Code: //Parameter 1 is the controller containing the form This way its one line, and the validation and form processing is handled in one central location with no extra work. If anybody sees how this could be a problem I wouldn't mind discussing it. The reason I like this over creating a library to handle forms is I don't have to add anything to the library every time I add a form element. Maybe I misunderstood your library but you do have to do that right?
[eluser]Colin Williams[/eluser]
Quote:The reason I like this over creating a library to handle forms is I don’t have to add anything to the library every time I add a form element. Maybe I misunderstood your library but you do have to do that right? Looks to me like he had this happen in the Controller. Using ME like that, are you running validation in 'controller: ![]()
[eluser]chuckleberry13[/eluser]
Yes its using ME. Sorry if that was a little vagiue. So here is the controller containing the form. We will call it Form Controller Code: class Form extends Controller { Now here is a view that I want to include this form in but only as a widget and on this view when the form passes validation I want it to redirect to page/newsletter. Code: <?= modules::run('form', 'page/newsletter', 'signup') ?> So my controller 'Form' holds the validation rules, the callback functions, handles the validation as well as serve up the form. It then redirects if the partial variable is present. Since I actually use this form via the URL (forms/signup) you'll notice the controller is set to spit out its own success page if there is not redirect variable present. Did that make any sense? This seemed to me the easiest way to reuse this form. What do you think?
[eluser]Colin Williams[/eluser]
Ah, that is clear. I forgot that you were embedding this form on many page views and thought you were calling the ME module controller to extend another form or share form widgets among many forms (you can see how I was dumbfounded!) I've been critical of ME recently, but I'm starting to see more usages where it makes certain things, especially this example, extremely simple to implement. And in other examples of ME Controllers, the way they returned data seemed odd to me, but I like more how your Controller checks the context in which it is called, and either returns data or generates output. It's a smart use of ME for sure, and I suppose I'm just focusing on the ways it can be abused. |
Welcome Guest, Not a member yet? Register Sign In |