Reputation: 1267
I need your assistant in showing the error messages in the proper messages component. In the form, I am using the messages component tag twice. The first messages component tag which is called messages, I am showing any error to the user that is validated from the main form.
Where I am also having a dialog in the same form that will validate if a variable is blank or empty, if it is blank,the error message should be shown in the messagesPDFST which is in the dialog
not in the main form. Currently any error it is shown in the first messages component which is messages even if the error is in the dialog
. Here is the code:
<h:form id="Requests">
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true"/>
<p:dialog id="Certificate1" header="Certificate" widgetVar="Certificate1" modal="true">
<div align="right">
<p:messages id="messagesPDFST" showDetail="true" autoUpdate="true" closable="true" />
<p:commandButton value="Download" ajax="false" actionListener="#{hrd.PDFSTAR}" >
<p:fileDownload value="#{hrd.fileSTAR}" />
</p:commandButton>
</div>
</p:dialog>
</h:form>
and the Bean Code:
public void PDFSTAR() {
if (refNo== null || refNo.equals("")) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter the Reference No."));
}
}
Upvotes: 3
Views: 6336
Reputation: 20691
To display a FacesMessage
for a specific component, you first need to
Queue the message, for the specific component:
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter the Reference No."));
The first argument where you have null
, is where you should have the client-side id
of the component you're looking to target
Specify the for
attribute on the message
component, limiting display of messages to that component only.
Putting it all together, you should now have
FacesContext.getCurrentInstance().addMessage("Requests:Certificate1:downloadButton", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter the Reference No."));
Where "downloadButton" is the id
of the <p:commandButton/>
<p:messages for="downloadButton" id="messages" showDetail="true" autoUpdate="true" closable="true"/>
<p:commandButton id="downloadButton" value="Download" ajax="false" actionListener="#{hrd.PDFSTAR}" >
<p:fileDownload value="#{hrd.fileSTAR}" />
</p:commandButton>
Unrelated to the actual problem
There's no need to be using <p:messages/>
in the dialog, if the message is to be displayed for a single component only - <p:message/>
will work just as well
Upvotes: 3