Building a Form Generator from Scratch

cwest on 2003-08-07T16:23:45

If I were to build one from scratch today, here's how I'd do it. I'm interested in thoughts. First and foremost, all the output I would generate by default would have to pass XHTML strict and government guildlines for accessibility. Second, each form element would be an object, with most object inheriting from the generic "Input" object. This would allow for the greatest flexibility. Form validation could be optional, but I wouldn't supply any defaults for you, with the possible exception of required fields. When you build a new form, it would create a new form object. This could be passed into your favorite templating engine and you could create the form yourself by selecting the part you want and iterating over the created form elements. If you don't want to use your own, you could call a method for the form to render itself. At this point, there should be a way to override default class attribute names on all the HTML elements created. And each element would know how to build itself. When it comes time to get information out of the form, you could first call the validate method. If that passes, you could use the form object to retrieve the values for each of the form elements. If it does not pass, you could choose to render the form, indicating that a fill-in attempt has been made but failed (or use your own templating system to do this). I don't think my form generator should automatically use CGI or Apache::Request to get parameters and do validation or sticky form behaviour, but I may be wrong. As for JavaScript error handling, maybe, maybe not. As for multi-page forms, it would be nice to handle this automatically, but it's probably not a priority (for me), but I may be wrong. I think that's about all I can think of.

Posted from caseywest.com, comment here.