By Peter Bell

Fussing with Forms (Part 2)

In the first part of this series I looked at how we could describe simple forms more concisely and DRYly. I'm not convinced that I made the case that a generator would be "the simplest thing that would work" - it all really comes down to how likely you would be to need to refactor your form markup during a project. Still, in this posting I want to look at some of the additional types of information required to describe a form to see if I end up with sufficient benefits to make it worthwhile to write a simple form generator that'll take XML or method calls and generate the appropriate HTML . . .

There are a couple of (very) small syntactic wins we could get with a form generator in terms of describing the form and hidden fields. Given that the majority of simple web apps use method="post", we could make that a smart default for a form thus making the method parameter optional and only used if you DIDN'T want "post". We could also tighten up the syntax for the many hidden fields we often use for managing state. So we'd take:

<form method="post" action="whatever.cfm">
   <input type="hidden" name="var1" value="val1">
   <input type="hidden" name="var2" value="val2">
   <input type="hidden" name="var3" value="val3">
</form>

And would get XML like:

<form action="whatever.cfm">
   <hidden name="var1" value="val1" />
   <hidden name="var2" value="val2" />
   <hidden name="var3" value="val3" />
</form>

or method calls that might look like:

myForm.setAction("whatever.cfm");
myForm.addHiddenField("var1", "val1");
myForm.addHiddenField("var2", "val2");
myForm.addHiddenField("var3", "val3");
myForm.render();

OR

myForm.setAction("whatever.cfm");
myForm.addHiddenField(name="var1", value="val1");
myForm.addHiddenField(name="var2", value="val2");
myForm.addHiddenField(name="var3", value="val3");
myForm.render();

Or if you're into writing parsers, a little language that might say:

Form
   hiddenField var1 val1
   hiddenField var2 val2
   hiddenField var3 val3

which looks tighter but will suffer from the problems I mentioned in my last article as this is really similar to method calls using implicit method ordering and while removing the name= and value= cuts down on what looks like visual cruft when you only have two obvious parameters, it makes the code completely unreadable as you start to get more and more parameters (and un-implementable if there are multiple optional parameters which can't be reliably distinguished based on mutually exclusive allowable value sets).

Well, I think that is a nice example of looking at different ways to encode the same intent and some of the associated trade offs you make as a language designer when choosing a concrete syntax for your DSLs, but even with the tighter mark up for field names and the small savings in hidden field descriptions this still isn't really worth the effort of messing with.

Of course, we could add some nice little features to such a generator such as passing a struct/hashtable or even a bean into a form so if we wanted to keep a set of state variables we could pass it into a form using a single call rather than having to write a loop. Let's say we wanted to pass all items in URL scope into hidden form variables. Instead of having to write a loop to go through the URL scope we might just write:

myForm.addHiddenScope(URL)

Again, not bad, but not a big enough win to justify the effort of writing, documenting and learning the generator. However, I think we *may* start to get a win when we start to encapsulate the implementation of rich form controls and client side validation. I think that'll have to go in article three in the series!

Comments
Yep, it's pretty hard to beat HTML for marking up forms. Plus there are about 100 million people who know it well. Looking forward to your take on forms "beyond HTML" aka rich client.
# Posted By Jaime Metcher | 12/20/07 4:43 PM
Not so much rich client as rich controller - WYSIWYG, date picker and the like. Stay tuned!
# Posted By Peter Bell | 12/20/07 8:46 PM
A good formbuilder would be great.
I am surprised it has not been integrated with Reactor.
It seems like a logical thing to add to an ORM API.
Looking forward to seeing what you come up with.
# Posted By Kevin Pepperman | 12/20/07 11:02 PM
@Kevin:
re "a good formbuilder": assuming any such thing exists. Have you used one? For my money, MS Access has one of the best form builders around - and it still drives me nuts.
# Posted By Jaime Metcher | 12/21/07 8:32 PM
I have used a few but nothing that really integrates well with reactor or transfer.

CFTL
http://cftl.riaforge.org/
works OK...but is quirky how the objects are created.

Formulate
http://formulate.riaforge.org/
Actually may have some potential for building forms automaticly with reactor Metadata.
But I have not had to much sucess in automating the form building process.

I did read that the scaffolding in fusebox generates some forms but I would really like to see something done within Reactor and the getObjectMetadata() function.
# Posted By Kevin Pepperman | 12/21/07 9:07 PM
@Kevin, I think Reactor is a persistence framework, so for me it would be really inappropriate for it to start to get knowledgeable about HTML and Forms. It'd be like ColdSpring generatng your H1 tags - not a great idea. There is scaffolding in Fusebox and Model Glue, and you should look at projects like Django (Python) and of course Ruby on Rails (Ruby) to see their approaches.

We have an in-house form synthesizer which works really well for us, but it really isn't that hard to create something custom that works the way you want it to. it's certainly a lot quicker than (ugghh) writing forms by hand!
# Posted By Peter Bell | 12/21/07 9:57 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.