Using Hibernate Constraint Annotations with Seam/JSF

Posted by & filed under Uncategorized.

Most if not all the Hibernate validators have a message attribute that can use an expression representing a key in a message bundle. The message bundle is defined and tied to the JSF Messages feature thru Seam configuration in the components.xml configuration.

<factory name=”m” value=”#{messages}”/>

<core:resource-loader>
<core:bundle-names>
<value>/WEB-INF/messages/system</value>
<value>/WEB-INF/messages/application</value>
</core:bundle-names>
</core:resource-loader>

So, the example below uses the @Pattern annotation. The regular expression (regex) value indicates that there must be 5 consecutive numbers to be valid. If validation fails, JSF/Seam looks-up the message by the key invalid_value_x in the message bundle application_en.properties and displays its value as the error message.

@Pattern(regex = “\\d{5}”, message = “{invalid_value_x}”)