Overview
Other MyFaces Extensions
- CODI
- Ext-Script
- [Orchestra]
- [Portlet Bridge]
Author: Rudy De Busscher
Reviewer: Gerhard Petracek
The messages which are shown when a validation fails can be customized in different ways. MyFaces integrates different constraint types (JSF based validators, Bean-Validation constraints, ExtVal constraints and any kind of custom constraints). The different constraint types might be a bit confusing at the beginning. This page shows an easy way to customize the violation messages of the constraints.
Just like most web applications, the messages are identified by keys which are used e.g. in resource bundles. The following table shows an overview of the keys:
Annotation |
Message Key |
---|---|
@Length |
javax.faces.validator.LengthValidator.MINIMUM |
@LongRange |
javax.faces.validator.LongRangeValidator.NOT_IN_RANGE |
@DoubleRange |
javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE |
Annotation |
Message Key |
---|---|
@Column |
field_required |
@Basic |
field_required |
@Id |
field_required |
@OneToOne |
field_required |
@ManyToOne |
field_required |
The BV spec. suggests to use the name of a constraint + the postfix ".message".
Key have the be marked with curly braces.
For example the @Null constraint uses: {javax.validation.constraints.Null.message
}
In the resource bundle the key is javax.validation.constraints.Null.message
The keys for the standard constraints are documented in the [BV spec.]
Annotation |
Message Key |
---|---|
@Required |
field_required |
@Pattern |
no_match |
Annotation |
Message Key |
---|---|
@Equals |
duplicated_content_required |
@NotEquals |
duplicated_content_denied |
@DateIs |
wrong_date_not_before |
@RequiredIf |
empty_field |
@EmptyIf |
field_not_empty |
As you can see there are 5 groups of annotations (incl. custom constraints with custom message keys) that can be used with ExtVal for validation. The validation strategies (the code which is responsible for making the actual checks or the delegation to existing validators) for the ExtVal annotations can be split up into 2 groups. The ones that use the JSF validators (like @Length) and those that are implemented by ExtVal itself (like @Pattern and @Required). So we have actually 6 groups that have small differences in 'resolving' their message. You can find the details in the next paragrapgh, but if you need a quick solution that works for any group, do the following:
<application> <message-bundle>com.mycompany.myprog.messages</message-bundle> </application>
<context-param> <param-name>org.apache.myfaces.extensions.validator.CUSTOM_MESSAGE_BUNDLE</param-name> <param-value>com.mycompany.myprog.messages</param-value> </context-param> <context-param> <param-name>org.apache.myfaces.extensions.validator.JPA_VALIDATION_ERROR_MESSAGES</param-name> <param-value>com.mycompany.myprog.messages</param-value> </context-param>
Now you can use the keys listed in the first section of this page to redefine the message(s). JSF differentiate between two kinds of messages (summary and detail). You can use the listed message keys + the postfix "_detail" for a detail message.
If you like to use other keys then the ones on this page, you can specify them via the attributes of a constraint.
ExtVal constraints use the attribute name validationErrorMsgKey
(it isn't a rule -> for custom ExtVal constraints you can use any name you prefer) or message
for BV constraints (that's a fixed rule). JPA annotations as well as annotations for JSF validators don't have a special attribute.