Domanda

I'm new to JSF, my question may be silly for you.. It is very much valuable for me..

<h:form id="form1">
    <p:messages autoUpdate="true" showDetails="true" />
    <p:dataTable id='form1ID'>....</dataTable>
        <p:inputText value="#{bean.name}" required="true"
            requiredMessage="Please Enter Name!" label="Name ">
        </p:inputText>
</h:form>

<h:form id="form2">
    <p:messages autoUpdate="true" showDetails="true" />
    <p:dataTable id='form2ID'>....</dataTable>
        <p:inputText value="#{bean.name}" required="true"
            requiredMessage="Please Enter Name!" label="Name ">
        </p:inputText>
        <p:commandButton value="Submit" update=":form1:form1ID"
            actionListener="#{mgmtBean.doCreateType}" />
</h:form>

I have two forms. when I click on form2 command button with empty fields, it will show error messages perfectly since i have added <p:messages autoUpdate="true" showDetail="true"/>.

The bad thing or surprising thing here for me is, it is showing error messages on top of form1 also may be because of <p:messages autoUpdate="true" showDetail="true"/> added in form1 and i'm trying to update form1 on command button click in form2. I don't want to show error messages on form1 when form2 is throwing validation error messages. How can i fix this ? I googled it out.. but couldn't find the solution.

Evironment i'm using is jsf-api-2.1.5 and primefaces 4.0 version and view technology is facelets(XHTML)
Hope it is clear..

Any idea is highly appreciated.

È stato utile?

Soluzione 3

I have fixed the issue by using the following code

<p:messages autoUpdate="true" showDetails="true" visibility="info"/>  

so it is just displaying info messages and other pop up error messages will not be showun.

Altri suggerimenti

Try to disable rendering of messages on the form1:

   <p:messages autoUpdate="true" showDetails="true" rendered="#{bean.renderedmessage}"/>

And in the bean, add the renderedmessage variable:

   public Boolean getRenderedmessage() {
   return renderedmessage;
   }

   /**
   * @param renderedmessage the renderedmessage to set
   */
   public void setRenderedmessage(Boolean renderedmessage) {
   this.renderedmessage = renderedmessage;
   }

And, for the doCreateType() method, add:

  public void doCreateType(){
  ..............
  setRenderedmessage(false);
  .............
  }

After the form1 is updated, you can choose some event or method, where you can setRenderedmessage(true);

What you're currently experiencing should indicate to you that you usually don't need two <p:messages/>s in the same JSF view; one is enough.

Regardless of the fact that you have two <h:form/>s on your page, there's still only one FacesContext associated with that view (The FacesContext object is a JSF construct that encapsulates all the information/data/components etc that's associated with a given JSF page request).

By default the <p:messages/> will display every FacesMessage that is queued within it's associated instance of FacesContext, regardless of the source of the message (well, almost: if you set globalOnly="true" on the <p:messages/>, you can change that behaviour).


That being said, you basically have two options:

  1. Get rid of the extra <p:messages/> and rely on (and appropriately position) only one to display all errors/warnings for that view

  2. Specify the for attribute, set to the desired component. Doing so, you'll guarantee that only one input component can trigger the display of its associated message component. For example

    <h:form id="form1">
       <p:messages for="inputName" autoUpdate="true" showDetails="true" />
       <p:dataTable id='form1ID'>....</dataTable>
          <p:inputText id="inputName" value="#{bean.name}" required="true"
              requiredMessage="Please Enter Name!" label="Name ">
          </p:inputText>
    </h:form>
    

    Note that I've set an id attribute on the <p:inputText/> component; this is necessary so you can use it in the for attribute for the messages component.

Further reading:

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top