Struts 2: Validating children collections using ValidationInterceptor + Visitor
I have a model object that has a collection of children in a header-detail relationship, with the header handled in one action class (and form) and the details edited 开发者_开发知识库in a separate action class (and form). I want to be able to do a full validation of the whole graph of objects from the header when I save the header object; I've already declared a visitor as such inside the header's -validation.xml
: (we're using XML validation in this project)
<field name="details">
<field-validator type="visitor" />
</field>
The detail class also has the proper validation.
We happen to also create/prepopulate this particular object graph from another graph; however, the prepopulation will leave some e.g. required fields as empty.
What happens is that only the header gets validated, since that's the form being submitted when the save method runs. Is it possible to force validation of the details as well (via the visitor I declared), when in fact the details don't have any form elements in the header form?
I want to keep it DRY and not have to do explicit validation inside the header action's validate() method.
Part of the problem is that when the validation runs, struts only sees the object as it was created from the form. If the list of details are not referenced in the form in some way, the xml validation won't be able to see that information in order to validate it.
One possible solution would be to include the detail information in the form through hidden fields. You could iterate over the list, and add a hidden field for each detail object. You would probably need some kind of custom data converter, unless you wanted a hidden field for each data member of the detail object.
Other than that, I can't see any way to do it solely through xml validation.
精彩评论