How do I attach a FacesMessage from the backing bean to a specific field in a ui:repeat?

后端 未结 2 335
小鲜肉
小鲜肉 2021-01-20 05:57

I have a form with a variable number of input elements, like this:


    

        
2条回答
  •  猫巷女王i
    2021-01-20 06:42

    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> fieldValidationMessages = new HashMap<>();
    

    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.

提交回复
热议问题