3 Replies Latest reply on Aug 5, 2009 9:03 AM by Miromir Vsevolodovich

    Validation messages HTML formatted, best practice?

    Miromir Vsevolodovich Newbie

      I'd like to inform user about validation problems with HTML-formatted messages, but if I use standard tag h:message, it looks like there is no HTML support in it.


                          <h:outputLabel for="email" value="Email" />
                          <h:inputText id="email" value="#{user.email}"/>
                          <h:message for="email"/>



      What is the best solution here to have the error message formatted?

        • 1. Re: Validation messages HTML formatted, best practice?
          Diego Y K Newbie

          I don't understand really well your question.


          What do you mean by HTML Support? You mean change font colors and etc?


          • 2. Re: Validation messages HTML formatted, best practice?
            Miromir Vsevolodovich Newbie

            I mean do not escape HTML output (like in h:outputText there is an attribute escape).


            I want for example to highlight the wrong letters in user input with red background, like this (wrong letters are b and e) :


            Some letteres are wrong in "a<span class="wrongLetter">b</span>cd<span class="wrongLetter">e</span>"



             <style>
             .....
             .wrongLetter{
              background-color : #FF3100;
             }
             ......
             </style>
            





            • 3. Re: Validation messages HTML formatted, best practice?
              Miromir Vsevolodovich Newbie

              I could not find the way for using standard h:message in my case, so I solved it another way, by using h:outputText instead.


              1) HashMap for custom validation messages :


              @SuppressWarnings("serial")
              @Scope(ScopeType.SESSION)
              @Name("msgFactory")
              public class WebMsg extends HashMap<String, String>{
                   
                   @Factory(value = "webmsg", autoCreate = true, scope = ScopeType.SESSION)
                   public WebMsg getMessages() {
                        return new WebMsg();
                   }
              
                   @Override
                   public String get(Object key){
                        String rez=super.get(key);
                        if(rez!=null)remove(key);
                        return rez;
                   }
                   
              }



              2) Put validation messages in webmsg instead of FacesMessages.instance().addToControl :


              //FacesMessages.instance().addToControl("email", "message");
              webmsg.put("email", "message");



              3) Use h:outputText instead of h:message


                                  <h:outputLabel for="email" value="Email" />
                                  <h:inputText id="email" value="#{user.email}"/>
                                  <h:outputText value="#{webmsg['email']}" escape="false"/>