5 Replies Latest reply on Jan 21, 2008 8:52 AM by cdupont

    Seam FacesMessages with its createFacesMessage method and i1

    fernando_jmt

      Hi everyone.

      I just developing JSF converters and JSF validators and you are noticed that both of them have a way to add a FacesMessage when throwing the Exception (ConverterException and ValidatorException respectively).

      I saw static method FacesMessages.createFacesMessage(..) which returns a FacesMessage, but that mehod does not works for me, because I wanna show an internationalized message (using messages.properties).

      So, what do you think to add a new method which allows us to get a FacesMessage using a resource bundle key?

      Something like:

      public static FacesMessage createFacesMessageFromResourceBundle(Severity severity, String key, Object... params)
      


      What do you think?

      or is there another way can I get a FacesMessage in the situation I describe above?

        • 1. Re: Seam FacesMessages with its createFacesMessage method an
          pmuir

          You can use:

          FacesMessages.instance().addFromResourceBundle(getClientId(facesContext), severity, key, params);


          • 2. Re: Seam FacesMessages with its createFacesMessage method an
            fernando_jmt

             

            "petemuir" wrote:
            You can use:

            FacesMessages.instance().addFromResourceBundle(getClientId(facesContext), severity, key, params);



            I tried what you suggest me before posting the message here. But it does not work.

            This because the ValidatorException has this signature when creating it:
            public ValidatorException(javax.faces.application.FacesMessage facesMessage)
            



            So, I need a FacesMessage in order to use it for the ValidatorEception constructor.

            This is was I tried:

             FacesMessages.instance().addFromResourceBundle(uiComponent.getId(), FacesMessage.SEVERITY_ERROR, MESSAGE_KEY, messageParams);
            
             throw new ValidatorException(new FacesMessage());
            


            But adding above code does not work. It validates, but no message appear in the page. So, FacesMessages.instance().addFromResourceBundle() does not work in this situation.

            I also tried this and this works:

            FacesMessage message =FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR, "The value (#0) is not equal with value of #1", messageParams);
            
            throw new ValidatorException(message);
            
            


            But in the above code the problem is that there is no way to create a FacesMessage from resource bundle. This is the reason because I suggest to add the method I mentioned before.


            Is there something I am missing?

            There is another way?

            Thanks in advance.



            • 3. Re: Seam FacesMessages with its createFacesMessage method an
              pmuir

              Seems like a good idea. Please put it in JIRA

              • 4. Re: Seam FacesMessages with its createFacesMessage method an
                fernando_jmt
                • 5. Re: Seam FacesMessages with its createFacesMessage method an
                  cdupont

                  My solution:

                  public void validate(FacesContext context, UIComponent cmp, Object value)
                  throws ValidatorException
                  {
                  if (ValidationUtils.validateEmail((String) value))
                  {
                  MemberManager memberManager = (MemberManager) Component
                  .getInstance("memberManager");

                  if (!memberManager.isUsernameAvailable((String) value))
                  {
                  String msg = ResourceBundle.instance().getString(
                  "validation.member.email.taken");

                  FacesMessage errorMessage = new FacesMessage();
                  errorMessage.setDetail(msg);
                  errorMessage.setSummary(msg);
                  errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);

                  throw new ValidatorException(errorMessage);
                  }
                  }
                  else
                  {
                  String msg = ResourceBundle.instance().getString(
                  "validation.member.email.invalid");

                  FacesMessage errorMessage = new FacesMessage();
                  errorMessage.setDetail(msg);
                  errorMessage.setSummary(msg);
                  errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);

                  throw new ValidatorException(errorMessage);
                  }
                  }
                  }