I have a form with a variable number of input elements, like this:
There is another option: Make your own replacement of the whole FacesMessages shebang. With blackjack. And...
Anyway, based on discussions with Johannes Brodwall we opted to avoid the whole visitTree mess and build our own messages mechanism. This included:
1) A ViewScoped bean containing a Map of Multimaps:
private Map
This takes an Object
as a field identifier (could be the respective bean itself, a UI component or even a String
generated at runtime inside the ui:repeat
. That identifier then can have an arbitrary number of String
messages on another arbitrary number of sub-fields. Very flexible.
The bean also had convenience methods for getting and setting messages on fields and subfields, and for checking whether any messages are stored at all (i.e. whether there were validation errors).
2) A simple xhtml include that displays error messages for a given field, replacing h:messages for...
And that's already it. The catch is that this runs during the application and rendering phase of the lifecycle instead of JSF's own validation phase. But since our project already decided to do bean validation instead of lifecycle validation, this was not an issue.