jsf-2primefacesuirepeatbacking-beans

how to set error message to p:message inside ui:repeat from backing bean


I have a form contains with inputText and message component.
I want to set the error message from the backing bean but keep fail to do it.

Below is my html code:

<h:form id="formId">
    <h:panelGrid id="repeater" columns="2">
        <h:outputText value="#{msg['label.appeal.case.reference.no']}" />

        <ui:repeat id="uirepeater" value="#{beanPage.list}" var="value" varStatus="status">
            <h:panelGrid columns="2">
                <p:inputText id="refNo" value="#{beanPage.list[status.index]}" />
                <p:message for="refNo" display="text" />                
            </h:panelGrid>
        </ui:repeat>
    </h:panelGrid>

    <p:commandButton id="btmAdd" actionListener="#{beanPage.addRow}" value="Add" update="@form" />
    <p:commandButton id="btmSubmit" actionListener="#{beanPage.submit}" value="Submit" update="@form" />
</h:form>

below is backing bean code:

public void submit() {
    FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "test", "test");
    FacesContext.getCurrentInstance().addMessage(":formId:uirepeater:refNo", msg);
    FacesContext.getCurrentInstance().addMessage(":formId:uirepeater:0:refNo", msg);
}

Solution

  • This will not work with ui:repeat. The actual ID of the inputText will not be "refNo" as you might think.

    The < ui:repeat > will ensure the uniqueness of the generated component's client ID by prepending it with the row index. It just renders the same component several times, not creating new components in the tree.

    You probably need to use c:forEach, which generates several components in the tree.

    For more info see:

    https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat

    How can I set id of a component/tag inside ui:repeat