2 Replies Latest reply on May 17, 2009 11:30 AM by Nick Belaevski

    rich:messages for=

    José Ventura Newbie

      Hello, I'm having difficulties using rich:messages to display all messages for a particular field, using a hardcoded message
      built inside a managed bean method. <h:messages for="someField"/> works, while <rich:messages for="someField"/> doesn't.

      I'm using richfaces-ui-3.3.0.GA and facelets-1.1.14 .

      In my .xhtml, I create a textfield, a message area and a submit button:

      ++ <h:messages /> ++
      ** <rich:messages/> **
      
      <h:form id="classificationForm">
       <h:inputText id="theText" required="true"/>
       <h:commandButton type="submit" value="Send"
       action="#{ClassificationBean.testMethod}"/>
      </h:form>


      Here's the code for the above method:

      public String testMethod() {
       FacesMessage fm = new FacesMessage( "hardcoded error!" );
       fm.setSeverity( FacesMessage.SEVERITY_ERROR );
       FacesContext.getCurrentInstance().addMessage( "theText", fm );
       return "";
      }


      If I leave the field blank and press the button, both h:messages and rich:messages show the "Required" error (OK).
      If I put something in the field and press the button, both h:messages and rich:messages show the "hardcoded" error (OK).

      Now I change it so they only show messages for a particular field:

      ++ <h:messages for="theText"/> ++
      ** <rich:messages for="theText"/> **


      If I leave the field blank and press the button, both h:messages and rich:messages show the "Required" error (OK).
      If I put something in the field and press the button, only h:messages shows the error. rich:messages doesn't show anything.

      I am trying to debug this to narrow the cause, but I'm lost on where to begin. Is the rich:messages behaviour implemented in a Java class? If so, which one? If it's not a Java class, where can I start looking for the cause?

      Thanks in advance!

        • 1. Re: rich:messages for=
          José Ventura Newbie

          update: I saw the code in the following classes: org.richfaces.renderkit.html.HtmlRichMessagesRendered and org.richfaces.component.UIRichMessages

          Seems the problem happens around line UIRichMessages:116 :

          (...)
          if (null != componentFor) {
           addMessagesForId(context, componentFor.getClientId(context), severenities);
          }
          


          (also, typo on "severenities", should it be "severities"?)

          I then had the idea to modify my method to refer to the textfield using its full ID (that is, together with the form id):

          public String testMethod() {
           FacesMessage fm = new FacesMessage( "hardcoded error!" );
           fm.setSeverity( FacesMessage.SEVERITY_ERROR );
           FacesContext.getCurrentInstance().addMessage( "classificationForm:theText", fm );
           return "";
          }


          This works on all cases. However, I'm still not sure that the behaviour should be different from the standard h:messages tag. Is this intentional?

          Thanks in advance!

          • 2. Re: rich:messages for=
            Nick Belaevski Master

            You should use clientId (e.g. "classificationForm:theText") when message are added.