dontcare
dontcare

Reputation: 975

multiple primefaces messages in templates

The Problem:

My Webapplication is build of multiple fragments and templates. The Header-Template has a "p:messages" to display application wide erros. In Addtion to that i have a Content-Template to display the content / body.

Now i have the following problem: I want to show content valdiation errors (e.g wrong date selected etc.) under the components in the Content-Template. If i send a facesmessage it won't only be displayed in the message part of the content but also in the header:

enter image description here

At the moment there are only two components which are going to be validated:

<p:calendar id="calendar1" value="#{doesn't matter}">
     <f:validateRequired></f:validateRequired>
</p:calendar>

<p:calendar id="calendar2" value="#{doesn't matter}">
     <p:ajax event="dateSelect" listener="#{onDateSelect}"
      partialSubmit="true" update="ContentMessages"/>
</p:calendar>

The messages tags:

    <p:messages id="GlobalMessages" showSummary="true" showDetail="false" closable="true" redisplay="false"/>

    <p:messages id="ContentMessages" showDetail="true" autoUpdate="true"/>

How i send the FacesMessage:

 FacesContext.getCurrentInstance().addMessage(componentClientId,
            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error:","Error Text"));

What i tried so far:

What i can't do:

Upvotes: 3

Views: 6896

Answers (1)

Emil Kaminski
Emil Kaminski

Reputation: 1885

As i cannot see the code of your templates i cannot tell if there's something wrong with them, but there should be no problem with targetable messages in primefaces.

Try to narrow the problem down by removing unnecessary code.

This example works 100%, and each message will only be displayed once.

<h:form id="form">
<p:messages for="somekey" />
<p:messages for="anotherkey" />

 <p:commandButton value="Message 1" id="m1" update="form" actionListener="#{playgroundController.addMessage1()}" styleClass="ui-priority-primary" />
 <p:commandButton value="Message 2" id="m2" update="form" actionListener="#{playgroundController.addMessage2()}" styleClass="ui-priority-primary" />

</h:form>

Bean:

public void addMessage1() {
    FacesContext.getCurrentInstance().addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO, "Sample info message", "Message 1"));
}


public void addMessage2() {
    FacesContext.getCurrentInstance().addMessage("anotherkey", new FacesMessage(FacesMessage.SEVERITY_INFO, "Sample info message 2", "Message 2"));
}

Upvotes: 4

Related Questions