Question

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);
}
Was it helpful?

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

OTHER TIPS

Try this I use primefaces 4.0, ViewScope, Ajax request. With p:component obtain the ClientId of p:messages atributte for="someComponent" and pass this value to bean through p:remoteCommand, later you can set the message from bean with ClientId. In some element add this

    onclick="rc([{name:'index',value:#{item.index}},{name:'msg1',value:'#{p:component('someComponent')}'}])"

Create p:remoteCommand to set the id

<p:remoteCommand process="@this" name="rc" action="#{bean.someMethod}"/>    

public void someMethod(){
FacesContext context = FacesContext.getCurrentInstance();
            Map<String,String> params = context.getExternalContext().getRequestParameterMap();
            int index=Integer.parseInt(params.get("index"));
            this.msg[index]=params.get("msg1");
}

Now you can set messages from bean:

FacesContext.getCurrentInstance().addMessage(this.msg[x], new FacesMessage(FacesMessage.SEVERITY_ERROR,null,"someMessage"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top