Search code examples
javajsffaceletscustom-errors

Using same message for some elements in facelets page


Now I am using JSF 2.0 and facelets to create some webpages. I have a login page which contains other stuff in it other than just the login panel.

Thing is I want to display the same message for the 2 input fields of the panel (Username and password) on top of the panel, but only 1 message for both. when I use <h:messages> the code is displayed correctly but along with all the other messages of other components as well. Because <h:messages> is Global for the entire facelet.

I tried using two <h:message for=""> and adding the same message for both of the input fields, but then the message is displayed twice.

the message would be "username and/or password is incorrect".

how can i do that displaying the message if any of them is incorrect ?


Solution

  • If you are adding those messages manually in your backing bean via FacesContext.addMessage(..) API, why can't you add 'em only for the username field or for the whole login panel?

    If you are using custom validators for this, you can take advantage of UIInput.isValid() method and try the following:

    <h:message for="password" rendered="#{username.valid}"/>
    <h:message for="username"/>
    <h:inputText id="username" binding="#{username}"/>
    <h:inputText id="password" binding="#{password}"/>
    

    or even

    <h:outputText value="msg['genericErrorMessage']" rendered="#{!username.valid || !password.valid}"/>
    

    On the other hand, it is not clear why you have to apply the same validation twice for both username and password fields.