aspose file tools*
The moose likes Struts and the fly likes Displaying Validation Errors with Indexed Properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Displaying Validation Errors with Indexed Properties" Watch "Displaying Validation Errors with Indexed Properties" New topic
Author

Displaying Validation Errors with Indexed Properties

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I've created a form which utilizes Indexed Properties and the user is, of course, able to edit multiple records at one time and then submit them all in batch. In my form bean, I have a validation routine that looks like this:



This validation works just fine and I can identify errors in any of the records. My problem is displaying those errors back to the user. In my JSP, I have tags that look like this:



I'd like to be able to put an html:errors tag with each of these, kinda like this:



But I'm not sure how to make this work. Because all of my fields are indexed, the names of those fields are things like singleToDoItem[0].title. If I try to put that tag within my logic:iterate tag, I get the error for a single item appearing for every record in the list.

Anyone know how I can get that error to appear for the one and only record that it really belongs to?

Thanks,
Corey


SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
For anyone that may be intersted, I've come up with a "solution" to this problem. It's not without its flaws, but it does seem to be working. Here's what I've done:

In my form bean, I have a collection of ToDoItemFormView objects. Those objects are really just a collection of Strings taht represents the data that would be found in my model objects. To that class, I've added a List called errorKeys, along with appropriate getters and setters for it.

In my form bean, I've modified the validate method in this way:



Note that, when I find a validation error, rather than adding an ActionError object to the returned collection, I add the key for that error message to the individual ToDoItemFormView object's errorKeys list. So, in this way, each ToDoItemFormView object has it's very own list of errors.

Finally, in the JSP, I do this in order to display the ToDoItemFormView objects:



Note the contents of the first <tr> element. That row displays any error messages, if they exist.

So, this seems to work - I can still display multiple errors for each record and I get them out of my ApplicationResources.properties file, which means those error messages can be easily modified. The downside is that, in essence, I've rewritten the functionality of the html:errors tag using some other means. It may not be perfect, but it seems to work and, after pulling my hair out over this for 4 or 5 hours, it's good enough for me.
Mark Blackledge
Greenhorn

Joined: Nov 04, 2005
Posts: 5
What if you made these changes:

errors.add("singleToDoItem[index].title", new ActionError("Edit.NoTitleSupplied"));

(you would have to generate the index value and put it in the string in your validation method)

and then change your iterate tag:
<logic:iterate name="ListItemsIndexedForm" property="items" id="singleToDoItem" indexId="index">
...
...
<html:errors property="singleToDoItem[${index}].title" />
<tr>....</tr>

</logic:iterate>
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Mark Blackledge:
What if you made these changes:

errors.add("singleToDoItem[index].title", new ActionError("Edit.NoTitleSupplied"));


Actually, I tried that, first. Unfortunately, I couldn't get it to work. In the JSP, you need a line like this (a you pointed out):



Unfortunately, this wasn't being evaluated out to this:



Rather, the html:errors tag would look for an error identified by the key "singleToDoItem[${index}].title" and that's obviously not what I wanted. Perhaps things are a little different, now - I'm using a slightly older IDE and do not have access to EL. So my actual line looked like this:



But it really should be the same. Regardless, I couldn't get that to work, so I had to come up with something else.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5212
    
  12

Originally posted by Corey McGlone:



But it really should be the same. Regardless, I couldn't get that to work, so I had to come up with something else.


i think it's a little late but instead of the line code you tried, i think you have to change that one into (i had similar problems and doing it this way solved it for me):



SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying Validation Errors with Indexed Properties