Dec 20th, 2005, 05:52 AM
Validating objects in lists
I'm having a bit of a problem trying to do something quite simple. On one of my pages I have a composite command object which has a list field. Each of the elements in the list, is, in turn a tree. I need to go through each of the elements in each of the trees and validate them. How do I do this in a Spring validator?
From what I've seen, in order to reject something in the Errors class I have to specify a field name. The field, in this case, would be some element in one of the trees. How do I specify this in the field parameter to the reject method? I was expecting to be able to use something like the bean utils expressions ( list.children.children.name), but it looks like Spring crashes as soon as it finds an index in the field expression.
Any ideas? Thanks
Dec 20th, 2005, 09:16 AM
Firstly, I guess you'd build the validator to suit your needs, and there's nothing that says that these need to be either small or simple class files. Sometimes you're just stuck with complex validation logic.
Secondly, to the errors/property question. You can either create a redundant form property and use the rejectValue() method, or simply call reject(). In the latter case you would then bind to path="formName.*" and iterate over the status.errorCodes property (I think). You could take it one step further with a custom tag and filter on certain error code patterns, so that only errors pertinent to your list of trees would be displayed.
Dec 20th, 2005, 09:32 AM
Thanks for your reply.
I wasn't worried about the validation part, I know I won't be able to get away from writing a somewhat complex validator. I was just trying to find a way to pass the object that actually caused the validation to fail back to the form in a way that doesn't force or hack the framework.
For simple command objects I see that this is done by passing the name of failed field. I was hoping that for more complex objects it can be done in the same way form data is bound into the command objects, through the patterns defined by the bean utils package. From what you are saying, it looks like this is not the case, though.