FormUtilities Plugin for ColdBox

CFML , ColdBox Add comments

A while ago, Brian Kotek released his FormUtilities.cfc. It's a very useful tool that allows you to create implicit arrays and structures from form fields which can be arbitrarily nested. Not only is this useful for dynamic forms where you don't know in advance how many sets of data will be submitted, but it can also be used to address the "all or nothing" nature of the entire event object being submitted along with the form in framework-based applications. Read more about this in Brian's blog post. I definitely saw how this could be useful in my ColdBox development, so I made a plugin out of it.

All I had to do was make some minor adjustments to the <component> tag and init() method of the CFC (which you can download from RIAForge):

So, I had to make sure the component extended the base plugin (coldbox.system.plugin). The init() method takes the ColdBox controller as it's only argument and then there is some plugin configuration info in the cfscript block (read more about this in the ColdBox docs). The updateFormScope and trimFields variables were arguments in the original CFC which I just set to "true". I dropped this CFC into my plugins folder and I'm ready to use it.

To use it, let's say I had a simple user registration form in my site. The form would normally look something like this:

In my user.handleForm event handler method, I would do something like this to populate a userBean with the values entered into the form:

That's all well and good. But we're throwing the entire request collection structure at the user bean (not just the form values). For simple cases like this, this approach might suffice. But what if, for example, you have a large, complex form that you want to use to populate multiple beans? The form utilities plugin allows you to implicitly create child structures within the request collection structure. So, we can modify our form this way:

 

Note that my form field names now all have a "user." prefix. Now in my handler, I can do this:

Since all my user form fields had names with the prefix "user." the plugin created a "user" struct within the request collection containing only the form values. That way I can just use those values to populate my bean. This is pretty cool, but I went even further to make parsing forms even more seamless. I wrote a parseCollection method in my request context decorator which calls the plugin for me:

Now the event handler code can be shortened to:

Keep in mind that I've just scratched the surface here on what the plugin can do. I'll refer you to Brian's post again on the ways you can use it to pass complex data structures with forms. I hope this also shows how drop-dead easy it is to integrate third party code into the ColdBox framework using its plugin architecthure.

7 responses to “FormUtilities Plugin for ColdBox”

  1. Stefan Says:
    This is a pretty cool plugin.
    But it can be even more usefull for you if you combine this with an interceptor. So it'll do this automagically for you using the preProcess() method in the interceptor.

    There's actually a third party interceptor that does this for you on the coldbox site. (Coldbox CodeDepot)
    http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbCodeDepot#ThirdPartyInterceptors
  2. Tony Garcia Says:
    @Stefan,
    Using an interceptor is a great idea and would make the functionality even more seamless. It shouldn't be too hard to use this plugin that way. Though I'm not sure that the Forminspector interceptor you pointed out can create arbitrarily nested structures and lists they way FormUtilities can.
  3. Dutch Rapley Says:
    @Tony, good stuff, just one thing on your label tag, the value of @for should be the same as your input's @id, not @name (i.e. for="FirstName"). When you make the change, you'll notice b/c when you click on the label, it'll put focus on the form field.

    BTW, the FormInspector interceptor will create nested structures.
  4. Tony Garcia Says:
    @Dutch,
    By "arbitrarily nested structures" I meant that, with FormUtilities you can create structures that are nested within structures that are nested within structures... as far as you want to go. You can also use brackett notation in your form field names to create arrays of structures (example: user[1].name, user[2].name), or arrays nested in structures nested in arrays.. you get the idea. Check out Brian Kotek's blog post that I linked to in my post for more info. I'm not trying to "one up" your interceptor (heck, I didn't even write FormUtils) -- just pointing out how they're different and I'd say the FormInspector will handle 99% of use cases.
    Thanks for pointing out my goof in the label tags. I went ahead and fixed it. Cheers!
  5. Aaron Greenlee Says:
    Thank you for sharing! Just what I was looking for!
  6. Tableau contemporain Says:
    hi, that's a nice post. i hope you will continue to do this :)
  7. tableau contemporain pas cher Says:
    hi, that's a metropolis billet. There is some mistakes but the main is here.

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Design and Icons by N.Design Studio