By Peter Bell

Fussing with Forms (Part 3)

In the first part of this series I looked at how we could describe simple forms more concisely and DRYly. In the second part I came up with some small syntactic wins, but really nothing to justify the approach. I'm hoping to strike paydirt (or strike out and move on) in this third posting which looks at encoding intent of rich fields and client side validations . . .

Often we want to use "rich" form fields that aren't provided for in HTML such as date pickers and WYSIWYG editors. We also often want to add client side validations to our forms. For a lot of common use cases, ColdFusion already has built in tags for implementing these. However, I have three problems with using the ColdFusion tags in my view markup.

Firstly, I'd really love to make my views scripting language independent so I could port my views between back end scripting languages automatically (yeah, I know, probably not too many people interested in that use case!). Secondly I'd like to be able to vary the implementation of my rich field controls. I may want to use the default ColdFusion implementation out of the gate, but I want to be able to change my implementation without having to edit all of my view screens - why should the interface (the tag or method call syntax) be tied to a specific implementation which may not meet my needs in the future? Finally, I'd like a consistent interface for describing all of my rich field controls irrespective of whether they're available in ColdFusion or whether they are something I want to add myself.

It seems to me that it might be nice to have a very simple wrapper around all of my fields allowing me to vary my implementations of each field type without having to change all of the calls to the interface (whether tag or method based). Similarly I'd like to be able to have a method for describing validations that I could then choose my preferred implementation for and where I could experiment with (for instance) different JavaScript libraries without having to make any changes to my view code.

I'm still not absolutely convinced that I'm going to get an ROI here, but I think I'm willing to spend a little time providing myself with my own templating language and generator to allow me to be able to vary the implementation of my views without having to change the templates.

There are some other potential benefits too. I could add simple tags for things like paging and even method calls, so I could replace my #Product.display("Title")# with just %Product.Title% so if I ever decided to go back to cfoutputting from queries, moved from generic to specific methods (from Product.display("Title") to Product.displayTitle()) or made other major architectural changes (which may be unlikely within a single project but much more likely across a software product line) I'd be set. I can also implement my own simple syntax for loops and hide variations into my controller API by abstracting that into the view generator and pulling it out of the templates. Yes, I have a feeling this extra step of indirection might fit quite well for my use case.

Comments
Just went through something similar. Mostly backend admin forms and related CRUD. I didn't go the path of a templating language, but rather a config file for the form and all its details, and nugget of code for edit, nugget for delete and nugged for add. All pretty reusable for any form variation.

Regarding the CF rich form fields, I found the one with the most trouble with the texteditor, for the reasons you describe. My approach was using JS to configure the controls on the fly. It can be implemented differently for each implementation and is independent of CF. There are problems with it, but at least you don't have to change CF's default config files (which you can't always do) or deploy an editor with every implementation (which is really silly and shouldn't be needed ideally).

The JS can be found here, which is really just calling the functions built into FCK.
http://www.usefulconcept.com/index.cfm/2007/11/28/...
# Posted By joshua | 12/21/07 10:12 AM
Hi Joshua,

That makes sense as an approach. I keep playing with different implementations of this - from custom tags to custom data types accessed using method calls from an IBO to a specific form bean. I think for me the goal of the full templating language is to make sure that the view code is independent of my design decisions as the problem I have today is that almost every project has custom view scripts and to port them all to a different version of my framework would require a bunch of manual effort (or some fairly smart Regex's, I guess!).

As for the cf texteditor, I moved to FCKEditor specifically because I know it'd be in CF8. Having done so, I'm now considering TinyMCE just because I had a number of clients having problems with the editor. I'm going to do another review of the state of the art in text editors, but I don't think FCK Editor will be our final choice - although I now they've also upgraded, so I'll see how it goes.

Nice Javascript though!
# Posted By Peter Bell | 12/23/07 5:44 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.