KBD

Keith Devens .com

Friday, May 16, 2008 Flag waving
"What is truth?" – Pontius Pilate
← Neat freewareLiveJournal →

Daily link icon Tuesday, June 4, 2002

Oh the tedium

Ever marvel at how much code it takes to do even simple stuff? How many times have you written data validation routines? Every little check to make sure some data you got was numeric, or that it looks like a date, or that the ID of the foreign key of the associated record you're trying to add actually exists before you blindly go insert into the table adds up. You wish you could just will it to be on the screen because you've done it so many times before, but you still have to type it out.

How many times have you written code like this?

<select name="show_hour">
<?php
for($n=1;$n<=12;$n++){
    echo 
"<option value=\"$n\"";
    if(
$show_hour == $n){
        echo 
' selected="true"';
    }
    echo 
">$n</option>\n";
}
?>
</select>

Oh, and not to mention the constant argument with yourself about how to structure your code (tradeoff between speed of coding, correctness, and maintainability), and between checking-every-single-thing-because-otherwise-the-code-just-isn't-"correct" and keeping yourself to realistic expectations of how robust you need to make your code (which obviously differs depending on the project).

← Neat freewareLiveJournal →

Comments XML gif

Keith Gaughan (http://hereticmessiah.weblogs.com/) wrote:

Uh-hu! That's why I'm planning to write those Interface Abstraction Toolkits for PHP and CF. Form building, automatic client- and server-side validation, enforcing a standard look and feel, blah, blah, blah, it'll do it.

I'm going to make the code freely available for anybody who wants to use it when it's reached a decent state.

BTW, shouldn't that be selected="selected"?

∴ Keith Gaughan | 4-Jun-2002 4:30am est | http://hereticmessiah.weblogs.com/ | #459

Keith (http://www.keithdevens.com/) wrote:

I noticed you were working on that Smiley. I've written things like that in the past, where I could automatically generate forms from information in a database, for example. But to make it work for the general case it seemed like you have to go through so much effort parameterizing everything that it becomes not worth it, and you still can't get it as flexible as you need.

Needless to say, I'm very interested to see what you come up with.

About the selected="selected" thing. You're right. I've been writing my HTML like I did above for years. They were boolean attributes, so I assumed the values must be "true" and "false". I just assumed that when I started, and never actually checked the spec to make sure -- "true" and "false" as values for boolean attributes was so obvious!

It seems silly that the value of the 'selected' attribute is "selected" while the value of the 'checked' attribute is "checked", even though they're both boolean attributes (same goes for multiple="multiple"). It also seems silly that you don't have symmetry, so you can have 'selected="selected"' but not 'selected="unselected"', which the more obvious 'selected="true"' and 'selected="false"' gives you.

But... the joke's on me Smiley. Thanks a bunch for pointing that out. I probably would have continued doing it that way for years if you hadn't stopped me.

Keith | 4-Jun-2002 5:06am est | http://www.keithdevens.com/ | #460

Keith Gaughan (http://hereticmessiah.weblogs.com/) wrote:

Dumb, I know, but I guess it was backwards compatibility the W3C were thinking about at the time.

Here's the basic scheme for the IAT (in PHP anyhow): the library consists of a set of classes that represent elements of the form.

There's the IATForm class, which forms the core of the toolkit. This dispatches messages to other classes to build the HTML JavaScript to represent the form and the validation code, and does the server-side validation too.

Each type of form element is represented by its own class, with some subtle differences like how textareas and textfields are handled by the same class, a textfield being used if the height is one and a text area otherwise.

Then there's the validation classes. These range from checking to see if the text entered is a date or if it fits a given regexp to checking that the correct range of checkboxes have been checked. This sort of thing is the primary reason for writing it - I hate writing validation code with a passion.

The last set of classes are more or less project bound - they're formatting stratagies for laying out the form. This is the sort of thing I've been writing a lot of at work so that I could reuse the various classes I'd written in VBScript and VB in different projects. It also means I can change the whole look and feel of an application in ways I wouldn't be able to do with CSS on it's own and would be too difficult to do with raw HTML.

For an example of a set of classes that are written along similar lines, take a look at QryListing, a set of VBScript classes to handle rendering listings of queries. There's a reasonable amount of code there, and it would be fair to say that it in some ways it seems bit overengineered, but as soon as the code was written, it infested the rest of the company's applications. And to thin I wrote it for my bug tracker!

IAT fits into the same kind of patterns as the QryListing suite. In fact, because PHP supports inheritance, it'll be much nicer to implement and extend than it's predessessor was. I'm also going to convert QryListing over. I've been meaning to for a while but never had a chance.

∴ Keith Gaughan | 4-Jun-2002 7:14am est | http://hereticmessiah.weblogs.com/ | #461

Keith Gaughan (http://hereticmessiah.weblogs.com/) wrote:

The thing is, has anybody ever actually written anything like this before. It'd be cool if they hadn't. The closest thing I can think of is WebForms in ASP.NET, but that seems to take a different stratagy and I was already thinking about IAT before I knew about WebForms.

ColdFusion has special tags which also produce client-side validation code, but they're a bit crap and I don't know anybody who ever uses those tags.

Know of anything?

∴ Keith Gaughan | 4-Jun-2002 7:21am est | http://hereticmessiah.weblogs.com/ | #462

Keith (http://www.keithdevens.com/) wrote:

I don't know enough about WebForms - I should learn. It's amazing how big .Net even long before anything's really released. It's like the technology isn't real yet, but everyone talks about it as if it were.

The only thing I know that does similar things to what you're doing -- making it easier to write form code, including both server-side and client-side validation -- is a project called SteelBlue. It's open source, but I don't think it's changed in a while. I think it was written by some PhD's. Might be worth taking a look at, but it's its own language/environment, it's not something like a class library that can fit into an existing language.

Keith | 4-Jun-2002 3:11pm est | http://www.keithdevens.com/ | #463

Adam Vandenberg wrote:

ASP.Net is a (kinda-sorta) welcome change for people who've been doing form hand coding in ASP all this time. The (sadly defunct) Encarta Experts project would have taken a bit less time with ASP.Net vs ASP, because ASP.Net has an easier to use framework for dealing with web forms.

Doing a form declaratively vs. response.writing out HTML is great, and being able to specify predicates for element validation is keen too.

ASP.Net as a whole, I don't know, I haven't had enough exposure to it. Some things are nice and simple, some things still feel big and clunky.

(The Conversatron, my side-coproject site in Python, is chock full of the Python version of the PHP snippet above.)

∴ Adam Vandenberg | 4-Jun-2002 6:24pm est | #464

Keith Gaughan (http://hereticmessiah.weblogs.com/) wrote:

SteelBlue: smells terribly like some elements of CF to me.

ASP.NET's not all that bad. It'd definitely a huge improvement over Classic ASP, AFAIC. It's gained a lot of the nice things about Java through .NET, and the fact that each page is a class pretty much rocks. It's actually bearable now.

WebForms is one of the best and least well-known parts of the new framework. I was rather chuffed when I saw it. It's really cool. It doesn't have formatting stratagies, but hey...

OTOH, ADO.NET is a bit clunky. Oh well...

Seeing as all the IAT code is going to be just a bunch of classes that plug into one another to allow you to create & process forms declaratively, it should be easy enough to convert over to Python. When I get the basic class framework done, I'll post it up on my site.

I'm really surprised nobody's ever done this before...

∴ Keith Gaughan | 5-Jun-2002 4:00pm est | http://hereticmessiah.weblogs.com/ | #466

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

May 2008
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

new⇒Maps of Iraq

my husband is in Scania too..he​says it's not too bad..he's been at​worse...

Cristy: May 16, 3:54pm

"IMDB for music"

IMDB for Music? It looks to be a​couple of years old...​http://MusicTell.co...

Ken Empie: May 14, 9:57pm

Generated in about 0.327s.

(Used 8 db queries)