I'm adding a faces message in an action method as below:
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Mail invalide", "Vérifier le mail");
FacesContext.getCurrentInstance().addMessage("connection:mail", message);
I'm expecting it to show up at the below position in the tree:
<h:body>
<p:layout fullPage="true">
<p:layoutUnit position="center" >
<p:accordionPanel multiple="true" activeIndex="0">
<p:tab title="Se connecter" >
<h:form id="connection">
...
<p:inputText id="mail" ... />
<p:message for="mail"></p:message>
...
<p:commandButton value="OK" action="#{login.validateMailPaswword()}" ajax="false" />
However, it didn't show up at the desired location. How is this caused and how can I solve it?
The client ID in addMessage()
must be valid in order to get the message to show up at the desired place. You already took into account that the <h:form>
is a NamingContainer
and thus prepends its component ID to the client ID of the children. However, you overlooked that <p:accordionPanel>
is also such one (and you should have spotted it by simply looking in the generated HTML output while figuring out the client ID for the message).
So, first give it a fixed ID too. E.g.
<p:accordionPanel id="tabs">
Then look in the generated HTML output for the client ID of the generated HTML representation of the input component you're referring in the message component. It'll look something like this:
<input type="text" id="tabs:connection:mail" ... />
So, refer exactly that ID in the addMessage()
.
context.addMessage("tabs:connection:mail", message);