Lavlove B
Lavlove B

Reputation: 31

How can we use multiple <h:messages> tags or <h:message> tags in a single JSF page?

My problem is that I have 2 forms in a single JSF page each having its <h:message> or <h:messages> tag. As we know the message/messages tags print any validation errors, so what happens is, suppose I leave the fields of any of the 2 forms empty it should print "Fields cannot be left blank" or some kind of message. It gives this message, but it gives twice as there are two forms. So I see the same error/validation message at each of the two forms.

So what I need is the <h:messages> or the <h:message> tag should display error/validation message only once for each of their respective forms!!

So any help would greatly be appreciated!!

Upvotes: 2

Views: 3632

Answers (1)

BalusC
BalusC

Reputation: 1108852

If you're using JSF 2, then you could just submit and update the form by ajax. This allows for partially updating the view.

<h:form>
    <h:messages />
    ...
    <h:commandButton ...>
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
</h:form>

<h:form>
    <h:messages />
    ...
    <h:commandButton ...>
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
</h:form>

Or if you can't/don't want to use ajax for some unobvious reason, or are still using the legacy JSF 1.x, then check in the rendered attribute of <h:messages> if the desired form is been submitted or not.

<h:form binding="#{form1}">
    <h:messages rendered="#{form1.submitted}" />
    ...
    <h:commandButton ... />
</h:form>

<h:form binding="#{form2}">
    <h:messages rendered="#{form2.submitted}" />
    ...
    <h:commandButton ... />
</h:form>

The <h:message> shouldn't have this problem by the way, in contrary to what you're implying in your question.

Upvotes: 3

Related Questions