Primefaces messages for validation and growl for the rest

13,336

Here is an example works as you want, you can change it according to your code :

in .xhtml

<h:form prependId="false"> 
     <p:growl id="growl" for="msg" showDetail="true" sticky="true" />  
     <p:panel header="Growl">  
     <h:panelGrid columns="2" cellpadding="5">  
        <p:outputLabel for="msg"  value="Message:" />   
        <p:inputText id="msg"  value="#{growlView.message}" required="true" />
        <p:outputLabel for="msg2" value="Message2:" />
        <p:inputText id="msg2" value="#{growlView.message2}" required="true" />  
     </h:panelGrid>  
     <p:commandButton value="Save" actionListener="#{growlView.saveMessage}" update="growl" />  
     </p:panel> 
     <p:messages id="messages" for="msg2" showDetail="true" autoUpdate="true" closable="true" />
</h:form>

in the for attribute for both <p:growl> and <p:messages> you can add whatever Ids you want just like for="id1 id2 .."

in bean

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean
public class GrowlView {
    private String message;
    private String message2;

    // setters / getters

    public void saveMessage() {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "summary", "details");
        FacesContext.getCurrentInstance().addMessage("msg", facesMsg);
    }
}
Share:
13,336
JuanDYB
Author by

JuanDYB

Updated on June 14, 2022

Comments

  • JuanDYB
    JuanDYB almost 2 years

    I have a JSF page with primefaces and I would like to use p:messages for the messages of the input validations and a Growl element for the rest of the things like notifications, errors ...

    At the moment I have a Growl with attribute for and I'm sending notifications with this for attribute.

    The problem is that I don't know which for attribute I have to put in the p:messages component. If I left it as null the notification messages are showing in Growl and on p:messages.

    Here you can see an example of my JSF.

    <p:growl id="growlNotifyNewReg" for="growlNotificationsNewRegister"
                             showSummary="true" 
                             showDetail="true"
                             autoUpdate="true"/>
    
    <h:outputText value="Validado"/>
    <p:selectBooleanCheckbox id="checkValidado" value="#{}" required="true"/>
    
    <h:outputText value="Temperatura (ºC)"/>
    <p:inputText id="Temperatura" value="#{}" required="true">
          <f:validateDoubleRange minimum="-50" maximum="60" />
          <f:convertNumber maxFractionDigits="1" />
    </p:inputText>
    
    <!-- Other inputs... -->
    
    <p:messages showSummary="true" showDetail="true" redisplay="false"/>
    

    When I want to send a notification I do like this.

    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "summary", "details");
    FacesContext.getCurrentInstance().addMessage("growlNotificationsNewRegister", facesMsg);
    

    This code sends messages to Growl component but also to messages component because it doesn't have for attribute. I want to know which attribute I have to put in messages component to show only the validations messages of the form.

    As you can see I put redisplay attribute to false. Can I put multiple ids in the for attribute of the p:messages?