FormUtilities CFC Now At RIAForge

Just a quick note that I've created a project at RIAForge for this CFC at http://formutils.riaforge.org/. This will make it easier to keep track of any problems or bugs if they arise. Thanks again for all the interest!

FormUtilities CFC Code Released (Create Implicit Arrays and Structures from Form Fields)

OK, folks were clamoring for the code so I've released it. You can download it from the Links sidebar pod. Please let me know if you find any problems with it. Assuming testing goes well, I'll release it as a project on RIAForge.

Implicit Creation of Arrays and Structures from Form Fields

I remember back in 1999 when I first started using Fusebox. The idea of taking all form and URL variables and keeping them in the attributes scope was brilliant. Now, all of the frameworks do something similar to wrap up the incoming variables, usually into an event object with event arguments.

However, one thing has always bugged me, and that is the "all or nothing" way that this is done. Many are the times I've had to pass the entire event arguments structure into a CFC just to get at a subset of them, perhaps the ones in the edit form for a User. I may want to use just the form fields for the User to populate a Bean or a Transfer Object. But getting at just those values can be difficult or somewhat kludgy, like passing in the whole struct as an argument collection and then relying on cfargument tags on the other side to actually whittle it down.

To help with this, I've created a CFC that will let you implicitly create arbitrary arrays and structures based on the names of your form fields. For example, this form:

<input name="user.id" type="text" value="user"><br />
<input name="user.name" type="text" value="user"><br />
<input name="user.password" type="text" value="user"><br />

Would be parsed and turned into this on the server:

Here are more examples to demonstrate the ability to do this as far as you need to go. Create arrays or structures within a structure, as well as a list of values if the fields have the same name (such as checkboxes):

<input name="user2.id" type="text" value="user2"><br />
<input name="user2.name" type="text" value="user2"><br />
<input name="user2.password" type="text" value="user2"><br />
<input name="user2.permission[2]" type="text" value="user2.permission[2]"><br />
<input name="user2.permission[1]" type="text" value="user2.permission[1]"><br />
<input name="user2.category.11" type="text" value="user2"><br />
<input name="user2.category.8" type="text" value="user2"><br />
<input name="user2.userType" type="text" value="user2type1"><br />
<input name="user2.userType" type="text" value="user2type2"><br />

Create an array of structures, that each contain their own arrays and structures. This can be handy for dynamic forms where you don't know how many sets of data will be on the page:

<input name="employee[1].name" type="text" value="employee[1]"><br />
<input name="employee[1].phone" type="text" value="employee[1]"><br />
<input name="employee[1].permission.2" type="text" value="employee[1].permission.2"><br />
<input name="employee[1].permission.1" type="text" value="employee[1].permission.1"><br />
<input name="employee[1].mode[2]" type="text" value="employee[1].mode[2]"><br />
<input name="employee[1].mode[1]" type="text" value="employee[1].mode[1]"><br />

<input name="employee[2].name" type="text" value="employee[2]"><br />
<input name="employee[2].phone" type="text" value="employee[2]"><br />
<input name="employee[2].permission.2" type="text" value="employee[2].permission.2"><br />
<input name="employee[2].permission.1" type="text" value="employee[2].permission.1"><br />
<input name="employee[2].mode[1]" type="text" value="employee[2]mode[1]"><br />
<input name="employee[2].mode[2]" type="text" value="employee[2]mode[2]"><br />

And lastly, a demonstration that the structure can be arbitrary if you had some need for it to be:

<input name="company[1].name.firstName.prefix" type="text" value="company[1]"><br />
<input name="company[1].phone[2].something[5]" type="text" value="company[1]"><br />

I'm considering cleaning this up and putting it up on RIAForge as a Form Utility CFC. Personally, I think it would be great just to pass in the User structure and know that all it has is the user-related form fields and nothing more. What do folks think? Would you use this as an alternative to passing in a whole event structure?

BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner