Fussing with Forms (Part 1)
Back in the old days, you'd often see forms embedded within tables, so the following markup would be common:
<tr>
<td>First Name</td>
<td><input type="test" name="FirstName"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="test" name="LastName"></td>
</tr>
</table>
Luckily, those days are mostly gone, but there are still different schools of though on the best way to mark up forms. Examples are a div based approach:
<label for="FirstName">First Name</label>
<input type="text" name="FirstName">
</div>
<div class="field">
<label for="LastName">Last Name</label>
<input type="text" name="LastName">
</div>
an ordered list
<li>
<label for="FirstName">First Name</label>
<input type="text" name="FirstName">
</li>
<li>
<label for="LastName">Last Name</label>
<input type="text" name="LastName">
</li>
</ol>
or the tighter (but harder to style)
<input type="text" name="FirstName">
<label for="LastName">Last Name</label>
<input type="text" name="LastName">
(If you want to know, I'm quite partial to the ordered list - that seems to me a reasonable balance providing some more elements for getting the styling just right while still being meaningful and reading well in most screen readers.)
The problem is that if you're building an application with lots of forms you have to choose and type a specific type of markup for every single field and would have quite a job if you needed to refactor from (say) the second implementation to the third towards the end of a project with hundreds of forms and thousands of fields (I'm sure you could perform an XSLT, but I don't think it'd be a trivial process). But why are we tying ourselves to a specific markup implementation in the first place and repeating it with every single field when the same core intent could more simple be described as:
Field name="LastName" Label="Last Name"
We might even be able to add simple conventions and for many of our fields just write:
Field Label="Last Name"
Using a convention (for example - your convention might vary) that the name is simply the title with non-alphanumeric characters removed. Of course, you'd still have to have name as an optional attribute, but why repeat yourself for the many fields where some such rule could be formulated - especially on green field applications?
So, in this simple case (we haven't got into drop downs and date pickers and WYSIWYGs just yet), it is clear that a DRYer, more concise syntax would allow us to both create forms more quickly and refactor their actual implementation more easily. The question then is what would be an appropriate syntax for describing forms? There are lots of levels of explicitness or implicitness to choose from.
If you love XML, I think the answer would be clear:
<field Caption="First Name" />
<field Caption="Last Name" />
</form>
A Pythonista might propose:
field(Caption="First Name")
field(Caption="Last Name")
A minimalist might suggest that if a form was only comprised of fields (let's make that simplifying assumption just for now) then you know each row is a field, indentation tells you when the form has ended, and using implicit named arguments all you really need to enter is:
"First Name"
"Last Name"
I'm a little worried by the minimalist approach as once you start to have more than a couple of arguments it becomes impossible to remember what the third parameter was, and the learning curve for such "as long as you know it, it makes sense" interfaces can be steep (I know - I wrote a couple of applications that way and even *I* find it hard to remember what all of the parameters were for some of the method calls without having to open up the appropriate beans and check their method signatures).
As for the difference between the XML and Pythonesque approaches (primarily a tag vs. a method call style syntax with the added twist of meaningful white space in the Pythonesque sample to save the brackets or end tags that would otherwise be required to denote the end of a semantic block), firstly I don't see much of a difference. OK, my heart stirs a little stronger at the "cleaner" code which is able to use a few less characters to encode intent, but both of these are DRY - unlike the earlier example, they aren't repeating redundant markup information with each field (whether it's in table cells, divs or an ordered list), so I really don't care that much. Especially as it would be very straightforwards to write an automated transformation to turn one into the other. Because of that, I'll just see which would be the easiest for me to implement.
So, to summarize, I've taken the example of a simple form and noted that we are often repeating information that we may need to refactor relating to the markup of a form. To be honest, as I step back and review this posting, I'm not sure that I ca justify the overhead of a generator for the amount of information I'm saving. I think if this was the only issue with forms, I'd be tempted just to select a markup (probably the ordered list) and to stick with that, arguing against refactoring to a different style of markup unless it was really essential for a given project. However, I have a feeling that I may get a few more wins by generating my HTML either from XML or from method calls, so I'm going to take the thought experiment a little further and see what shakes out. In the next posting I want to look a little more at some of the other information that would be required to fully describe a real world form.



Good post.
I often feel that way about forms myself. It takes a lot of repetitive code spanning a lot of pages to implement good forms.
Matt Quackenbush just released some interesting custom tags for forms. Have a look at http://www.quackfuzed.com/index.cfm/2007/12/16/Col...
for an introduction into the problem Matt solved.
DW
I have a "big picture" question. I know a lot of what you talked about involves making things easier for people to build just front-end parts of your application. Things like the CSS framework and describing the layout via XML... I guess the question is, what is the end goal? Do you want to eliminate the need for front-end developers? Do you want to give them a starting point? How does this mix with active vs. passive generation? What happens if a new field is added to the DB and a form needs to be updated?
Best case scenario, how do you see the future of form development?
In a perfect world I think your framework of choice should be able to give you the option to go with the least amount of code required to create a form such as calling something like createForm(fields="firstName,lastName,email"). What display string to display for each of these fields should be kept in the Model along with all other details like maximum length and so on.
It's also important that the framework doesn't lock you in to doing this all the time but also allow you to go down a level when necessary. The next level down would be to have one method call per field. The lowest level would be to code the HTML yourself.
I'm actually surprised you're even considering "hand coding" your forms since after following your blog for a while I know that you keep a lot of meta data about your tables / classes which means that you should quite easily be able to just generate a form, right?
By the way, I prefer the DL / DT / DD combination myself when doing forms. it's the one that makes the most sense to me semantically (since it relates the label and input elements with each other).
@Ben, The end goal is to be able to build quality web applications as quickly and cost effectively as possible - to be able to "build everything" and "change anything". Why have a programmer doing data entry? Why need a css gurur for styling every page? Why write all of your CRUD queries by hand? Why write basic getters and setters when you can generate or synthesise them? Why re-design your architecture from scratch for each project when most of them could use a common structure? It is all about removing inefficiencies from the process of recommending, quoting, specifying, building, testing, deploying and maintaining web applications, while still being able to drop in custom code anywhere so you don't have the limitations of a boxed solution like Miva merchant or OS Commerce which can be extended, but have certain specific things that you can't change. None of this eliminates front end developers, but it should allow them to build 10 quality sites a week instead of 2 a month. As for active/passive generation, those are just two of the tools in the kitbag to get my outcome of quicker application development. In a green field application, I wouldn't add a new field to the database. I'd add a new property to the business object. The system would then add the appropriate field to the db and make it available.
I am down with all the stuff you are talking about in terms of making application development more efficient; sorry if I was not clear about that - why write code when you can generate? Exactly. I was quite specifically asking about what the ultimate fantasy was about how / where / when a front-end developer would fit into the equation?
Meaning, do you have a front-end generated before any front end developers are brought in? If they make edits, are they making it to a config type file or to a template type file? If a relationship in the DB goes from 1:1 to 1:N and the front end needs to change from a single select box to a mutli-select box, who makes that change?
I don't mean to ask as an attack; I am a very goal-oriented developer and if I cannot see a goal, I cannot find a path. My problem here is that I cannot at all begin to answer any of the questions that I just asked - the ability to visualize the right goal is simply beyond the ability of my brain :)
Lots of great questions. My ideal is as follows. Describe all screens declaritivelty and use that to passively generate rough cut of views which front end developer will tweak. Abstract as many settings into declaritive metadata so (for instance) in my current system I have a Product.field("CategoryList") method that displays the appropriate field for a category list as described within the product object, so you'd change the metadata and wouldn't need to mess with the front end code at all.
It's really just an incremental approach of saying "what changes frequently and how can I speed up and de-skill the process of changing it". I've just got a lot of incremental improvements lying about as I've been focused n this for years!
That is actually my current approach. I just load a bean with metadata from the model and a PropertyNameList that a generic form uses for display. The problem with that is that I now have pretty tight coupling between my form and model so my form system really required my whole framework - it is very tight, but it is somewhat brittle. What I'm trying to ado is add an intermediate level of abstraction into a forms system that I could use anywhere and that isn't as knowledgeable about moy framework as my current system.
As always it's trying to balance flexibility with efficiency. I do have all three levels you discussed. I have a generic form template that loops through the property name list in a field - primarily for my admins, I have a Product.field("Title") for displaying the title field HTML, and I have the ability to hand write form fields. The problem is coupling between my bean and my form processor and I'm looking to see if I can offer more, finer grained layers of abstraction.
Great point about DL/DT/DD - I KNEW I'd forgotten one of the possible approaches!
Right, I keep forgetting that you have the whole concept of a "data type" which includes field rendering. Even if you could do *nothing* else, certainly, I think even a one-time generation of screens would help tremendously in application building.