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.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.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].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].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?






By the way, PHP won't create arbitrarily nested arrays and, more importantly, key-value pairs like this. Nice try though!
@Adam: yep, that's the whole idea. Automatically creating a top-level struct is nice, but I wanted the ability to do anything since there's no telling how you want to group your fields.
Does the input naming affect javascript referencing in things like qForms? I'm assuming not if this type of thing is popular in rails already.
For example, this input...
<input name="employee[2].mode[2]" id="employee[2].mode[2]" type="text" value="employee[2]mode[2]"><br />
can be referenced as so...? document.getElementById('employee[2].mode[2]').value
alert(document.forms['testform'].elements['company[1].name.firstName.prefix'].value);
alert(document.getElementById('company[1].name.firstName.prefix').value);
I like this approach better than Mach-II's event-bean deal as I felt like I wasn't keeping my M, V and C well separated when using that.
By the way, the last captcha characters were 'mat'. That was pretty slick.
Although these days with double bite characters in play this should not be a problem, however I'd check it out before people implement it in a mainstream app.
I sort of take the "Gmail approach", and my view would be that if someone is using a browser that is that old or quirky, I really don't care if they can use my app or not.
Post the code!!!! I'm itching to start using this in an application I'm building right now. I'll beta test for you.