Catfish
Catfish

Reputation: 19294

primefaces trying to add a p:message for validation errors and p:growl for success messages

I have a page with a dialog where a user can add a project. I want the server side validation error messages to be displayed in a p:message tag, but if the validation passes, i want the success message to be displayed in a p:growl tag and the dialog to close.

How can i achieve this? Currently i can get all messages (error or success) to appear in either all p:growl, all p:messages, or all in both.

<p:growl id="growl" showDetail="true" life="3000" autoUpdate="false" />

<p:dialog id="projectsDialog" header="Add Project" widgetVar="projectsDialog"  modal="true">

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

    <h:form>

      <h:outputLabel for="investigator" value="Investigator" />
      <p:selectOneMenu value="#{ppBacker.investigatorKey}" id="investigator" size="1" styleClass="listBoxMedium" >
      <f:selectItems value="#{ppBacker.investigators}" />
      </p:selectOneMenu>            

      <h:outputLabel for="startDate" value="Start Date" />
      <p:calendar value="#{ppBacker.startDate}" id="startDate" mode="popup" navigator="true" required="true" requiredMessage="Start Date is required" />                    

      <h:outputLabel for="endDate" value="End Date" />
      <p:calendar value="#{ppBacker.endDate}" id="endDate" mode="popup" navigator="true" required="true" requiredMessage="End Date is required" />

      <p:commandButton value="Add Project" id="s1Btn" type="submit" oncomplete="handleLoginRequest(xhr, status, args)" actionListener="#{ppBacker.addProject}" update=":growl">
      <f:setPropertyActionListener target="#{ppBacker.grantKey}" value="#{ppBacker.grantKey}" />
      </p:commandButton>           

      <!-- this jquery closes the add project dialog if the server side validation is successful -->
      <script type="text/javascript">  
        function handleLoginRequest(xhr, status, args) {  
            console.log('args.success = '+args.success);
            console.log('args.validationFaile = '+args.validationFailed);
                if(args.validationFailed || !args.success) {  
                  jQuery('#projectsDialog').effect("shake", { times:1 }, 100);  
            } else {  
              projectsDialog.hide();  

            }  
        }  
    </script>  

    </h:form>

My ppBacker.addProject method

public void addProject(){

    RequestContext context = RequestContext.getCurrentInstance();  
        FacesMessage msg = null;  
        boolean success = false;  

        // validate the investigator, start date, and end date are set
        if(!getProject().getInvestigatorKey().equals("0") && getProject().getStartDate() != null && getProject().getStartDate() != null) {  
            success = true;
        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Project Added Successfully", ""); 

        } else {  
            success = false;    
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Failed", "Investigator, Start Date, and End Date are required");  

        }  

        System.out.println("success = "+success);
        // I'M GUESSING THAT I CAN SOMEHOW USE THE FIRST PARAMETER OF THIS TO ATTACH MY MESSAGE, BUT I'M NOT SURE
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("success", success);  

}

Upvotes: 2

Views: 8468

Answers (1)

BalusC
BalusC

Reputation: 1108722

Make use of "globalOnly" feature. On FacesContext#addMessage() call, give all error messages a non-null client ID (e.g. the one of the input component, or the button component or even the form component) and give all success messages a null client ID.

This way you can get away with:

<p:messages ... globalOnly="false" />
<p:growl ... globalOnly="true" />

An alternative is to rely on #{facesContext.validationFailed} in the rendered attribute. When manually adding an error message, call FacesContext#validationFailed().

if (error) {
    context.addMessage(null, errorMessage);
    context.validationFailed();
} else {
    context.addMessage(null, successMessage);
}

Upvotes: 2

Related Questions