• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying Validation Errors with Indexed Properties

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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):

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic