I have a JSF 2.1 + PrimeFaces 3.3 page:
<h:form id="particluar">
<p:message id="msg" globalOnly="true" display="text"/>
<h:panelGrid columns="2" id="test">
<h:panelGrid id="panel2" columns="3">
<p:inputText id="name1" value="Student.studentID" required="true"/>
<p:commandButton value="Check Name" actionListener="#{Student.abc}" process="name1" update="panel2" />
<p:message id="msg1" for="name1" display="text"/>
</h:panelGrid>
<p:inputText id="name2" required="true"/>
<p:message id="msg2" for="name2" display="text"/>
<p:inputText id="name3" required="true"/>
<p:message id="msg3" for="name3" display="text"/>
</h:panelGrid>
<p:commandButton value="submit" actionListener="#{Student.xyz}" update="particluar" />
</h:form>
I'm trying to manually add faces messages when the name is invalid:
public void xyz() {
if (name1.equals("primefaces")) {
FacesContext.getCurrentInstance().addMessage("msg1", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid Name", "Invalid Name"));
}
if (name2.equals("primefaces")) {
FacesContext.getCurrentInstance().addMessage("msg2", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid Name", "Invalid Name"));
}
if (name3.equals("primefaces")) {
FacesContext.getCurrentInstance().addMessage("msg3", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Invalid Name", "Invalid Name"));
}
}
But they do not show up and the server log keeps printing the following warning:
There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered
How do I show them in the page?
And, a second question, if I submit the form with three empty input fields, it shows "required input error message" in each <p:message>
associated with the <p:inputText>
. How do I show them in the <p:message globalOnly="true">
?
How do I show them in the page?
You need to specify a valid client ID. The client ID is not the same as component ID. The client ID is whatever you see in the JSF-generated HTML output. Also, the client ID should be the one of the input component, not of the message component.
So, given a
<h:form id="particular">
<p:inputText id="name1" />
<p:message id="msg1" for="name1" />
</h:form>
the input component has the client ID particular:name1
(rightclick page in browser and do View Source to see it yourself). So, the message should be attached on exactly this client ID.
context.addMessage("particular:name1", message);
And, a second question, if I submit the form with three empty input fields, it shows "required input error message" in each
<p:message>
associated with the<p:inputText>
. How do I show them in the<p:message globalOnly="true">
?
The <p:message>
is not the valid component for that, you should use <p:messages>
.
<p:messages id="msg" globalOnly="true" display="text"/>
The globalOnly="true"
attribute means that only messages with a null
client ID will be shown. So, you should just add exactly such a message.
context.addMessage(null, message);
Unrelated to the concrete problem, the non-global messages ("Invalid name") which you're trying to add there should actually be done by a fullworthy validator. See also How to perform validation in JSF, how to create a custom validator in JSF for a concrete example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With