Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update message components associated with <p:commandButton ajax="false">?

I need your assistant in updating the <p:message> component in a JSF page once the non-ajax <p:commandButton> is clicked. Currently in the case, I need to validate if the employee number is null, then show the error message in the message component, else, nothing to be shown. But now,the form is not showing anything once the <p:commandButton> is clicked.

Below is the JSF code:

<h:form id="form">
    <p:inputText value="#{pdf.refNo}"/>

    <p:message id="message" for=":form:cmd2" showDetail="true"/>

    <p:commandButton id="cmd2"
                     value="Validate"
                     ajax="false"
                     actionListener="#{pdf.validate}"/>

</h:form>

And the Java bean is:

public void validate() {
    if (refNo.equals("") || refNo == null) {
        FacesContext.getCurrentInstance().addMessage(":form:cmd2", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Please enter the Employee No."));
    }
}

So is the above code is correct and how to show the message once the button is clicked?

like image 924
99maas Avatar asked Apr 01 '26 18:04

99maas


1 Answers

The issue was solved by using the for in order to specify the target component. So in the above case, I referred to the command button, so we it is clicked, a specific message component should be updated. This can be done by:

<p:message id="message" for="cmd2" showDetail="true"/> or

<p:message id="message" for="form:cmd2" showDetail="true"/>
like image 160
99maas Avatar answered Apr 04 '26 08:04

99maas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!