Search code examples
jsfbean-validationomnifaces

Omnifaces validateBean: can not get showMessageFor attirbute to work


I am using Omnifaces <o:validateBean /> to use JSR303 bean validation with a class level constraint. My question is based on a former question about how to attach message to specific components. Since Omnifaces 2.6 there is a showMessageFor attribute which solves my question at that time completely (so big thanks to the developers of Omnifaces library ;-). My problem is that I can not get it to work.

Here is my setting. I have three input fields, two of them are coupled in a class level constraint (weddingDay must be smaller than silverWeddingAnniversary; I could attach more code if necessary).

@DayIsBeforeAnniversary
@Entity
public class Person implements Serializable
{
    private String name;

    @Temporal(TemporalType.DATE)
    private Date weddingDay;

    @Temporal(TemporalType.DATE)
    private Date silverWeddingAnniversary;
    ...
}
<h:form id="main_form">
    <p:messages />

    <p:outputLabel value="Name" for="id_name" />
    <p:message for="id_name" />
    <p:inputText id="id_name" value="#{personController.person.name}" /><br />

    <p:outputLabel value="Wedding Day" for="id_wedding" />
    <p:message for="id_wedding" />
    <p:calendar id="id_wedding" value="#{personController.person.weddingDay}"  /><br />

    <p:outputLabel value="Silver Wedding Anniversary" for="id_anniversary" />
    <p:message for="id_anniversary" />
    <p:calendar id="id_anniversary" value="#{personController.person.silverWeddingAnniversary}"  /><br/>

    <p:commandButton 
        value="test" 
        action="#{personController.navigate()}" 
        update="@all"/>

    <o:validateBean 
        value="#{personController.person}" 
        showMessageFor="id_wedding id_anniversary" />
</h:form>

So my intention is to show the validation message in the <p:message/> tags corresponding to the two <p:calendar/> tags of which I specified the ID (showMessageFor="id_wedding id_anniversary").

The actual outcome is a bit different. Two messages show up in the general <p:messages/> tag. And all input fields are red highlighted (even the input for the name; every input and label has class="ui-state-error"). Actually there is not much difference when I leave out the showMessageFor attribute completely (except there is only one message rendered in the <p:messages/>).

Result

So what am I doing wrong respectivly how can I tell the error messages to appear in the two specific <p:message/> tags and how can I prevent the input field for the uninvolved name to become red?

(using Omnifaces 2.6.8 on Glassfish 4.1.1)


Solution

  • The problem is 2-fold.

    1. The showMessageFor attribute unintentionally interprets the client ID as the full client ID. Below approach will work for you.

      <o:validateBean 
          value="#{personController.person}" 
          showMessageFor="main_form:id_wedding main_form:id_anniversary" />
      
    2. The showMessageFor isn't used to mark only specific input fields as invalid. Unfortunately, there is no work around for that other than grabbing <o:validateOrder> instead.

      <o:validateOrder
          components="id_wedding id_anniversary"
          message="You cannot celebrate wedding anniversary before being married"
          showMessageFor="@all" />
      

    Both issues are fixed in 2.6.9 as per issue 446.