Blogging Roller

Dave Johnson on open web technologies, social software and Java

Extending the Struts tags to show required fields and validation errors.

I'm working on extending the Struts HTML form tags so that they are aware of the Struts Validator. I'm doing this to meet these two requirements:

  • All fields that are required fields will be indicated with a red star or some other indicator.
  • All fields fields that have failed Stuts Validation will show up with a red box or some otner error indicator.

I've already made some progress on this and I have a good solid proof-of-concept, but I get this nagging feeling that I am reinventing the wheel here - even though googling has turned up nothing.

Is the extended-tags approach the best way to meet my two requirements above? Has this work already been done for Struts - that is - am I reinventing the wheel?

Comments:

Awesome idea! Definitely a feature which should have been there all along! Using stylesheet classes, you could change the background color of the input widet (input box, checkbox, whatever) to red. This technique would reduce the chance of affecting layout than, say, inserting a red asterisk before or after the field.

Posted by Bryan Field-Elliot on January 14, 2004 at 02:09 PM EST #

It has been done by Erick Hatcher for his book Java Development with Ant and I believe Matt Raible has done some work on one for his AppFuse application. Both versions are available for downloading.

Posted by Joe O'Pecko on January 14, 2004 at 03:17 PM EST #

Joe is right, I grabbed Erik Hatcher's LabelTag and I'm using it in AppFuse and Struts Resume. To see it in action, login to http://raibledesigns.com/resume (tomcat/tomcat), go to "Edit Profile", turn off JavaScript and try removing some of the required fields, then saving.

Posted by Matt Raible on January 14, 2004 at 03:42 PM EST #

Erik's LabelTag is not quite what I'm looking for. I don't want to have to add another tag to every field on my JSP pages, I want the existing Struts HTML field tags to auto-magically indicate required fields and error fields.

By extending the Struts HTML field tags, in theory, I'll be able to change one taglib directive and get the new behaviour.

Posted by Dave Johnson on January 14, 2004 at 03:59 PM EST #

This is a similiar approach to the way Webwork 2's custom tags handle fields. While they do not encapsulate required/not required, they do encapsulate the field errors. So its definately a workable approach.

Posted by Patrick on January 15, 2004 at 09:50 AM EST #

What you're after seems a lot like Turbine's Intake service that provides for a $field.isValid() call inside Velocity macros. Personally I wish that Struts could take the validation from Turbine, and add a decent persistence engine to make it THE web framework. Configuring Intake is far less aggrivation than the Struts Validator.

Posted by Cameron Gray on January 15, 2004 at 06:00 PM EST #

Might I ask why you don't want to use the Label tag? Just curious...

Posted by Hans Gerwitz on January 16, 2004 at 08:43 AM EST #

Hans: as I said "I don't want to have to add another tag to every field on my JSP pages." I have an existing set of JSP pages and, if possible, I'd like to get this new "indicate required fields" behavior without modifying those pages.

Posted by Dave Johnson on January 16, 2004 at 09:23 AM EST #

After some more investigation, I've concluded that Erik Hatcher's LabelTag, or something very much like it, is the way to go if you need to indicate required fields and fields that have error conditions.<p /> I have some other requirements and so my solution will probably include both a LabelTag and extended Struts form field tags.

Posted by Dave Johnson on January 17, 2004 at 12:53 PM EST #

Post a Comment:
  • HTML Syntax: Allowed