Fussing with Forms (Part 2)
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:
<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:
<hidden name="var1" value="val1" />
<hidden name="var2" value="val2" />
<hidden name="var3" value="val3" />
</form>
or method calls that might look like:
myForm.addHiddenField("var1", "val1");
myForm.addHiddenField("var2", "val2");
myForm.addHiddenField("var3", "val3");
myForm.render();
OR
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:
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:
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!



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.
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.
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.
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!