2

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!!

thecoshman
  • 7,772
  • 5
  • 52
  • 77
Lavlove B
  • 31
  • 1
  • 4
  • I warn you using two forms in a single page because ie does not support this feature – olyanren Oct 26 '12 at 08:09
  • 1
    @mucayufa That would be a major bug in IE if it wouldn't support two forms on a page. Maybe you aimed at nested forms which is no valid HTML and can/will lead to errors in all browsers. – Matt Handy Oct 26 '12 at 08:13
  • Are you using nested forms? Please provide a sample jsf code of your page. Did you try ` – prageeth Oct 26 '12 at 08:47
  • @Matt ı do not mean nested forms. I used two separete forms in a single page, but ie compatibility problem came out. – olyanren Oct 26 '12 at 14:48

1 Answers1

3

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.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • @BalusC : When i use your second solution i get the following exceptionn. `javax.servlet.ServletException: Component ID j_idt171:j_idt99 has already been found in the view.` – Rasoul Taheri Dec 04 '13 at 12:08
  • @Rasoul: apparently you bound it to a shared bean property for some reason. Do not bind it to a bean and absolutely not to a property which is shared by multiple components. The code in answer is complete as-is. Do not use `#{bean.form1}` or so. Just use `#{form1}`. See also http://stackoverflow.com/questions/14911158/how-binding-attribute-in-jsf-works – BalusC Dec 04 '13 at 12:10
  • @BalusC your guess is true.I have 2 form in one jsf page and use a shared bean for that forms. I set id for both ``. form1Mess and form2Mess. It return me following exception: `Component ID contact:form1Mess has already been found in the view.` you thinks there is not any solution except separate forms bean? – Rasoul Taheri Dec 04 '13 at 12:32
  • @Rasoul: Do not use `#{bean.form1}` or so. Just use `#{form1}`. You do not need those components in the bean at all. – BalusC Dec 04 '13 at 12:33
  • @BalusC : I use just #{form1}. see my code : `` and `` – Rasoul Taheri Dec 04 '13 at 12:34
  • @Rasoul: Then it means that you've multiple components bound on the same variable in the same view. Perhaps via include/tagfile/composite? – BalusC Dec 04 '13 at 12:38
  • @BalusC: no. i don't have. i think error refer to first h:messages. but this is not important thing. i change my way. i must say big thank to you. you help me and other several time. your are very good. thanks. – Rasoul Taheri Dec 04 '13 at 13:18