You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Internationalization

Author: Rudy De Busscher
Reviewer: Gerhard Petracek

Introduction

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.

Message keys

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:

ExtVal Annotations for JSF Validators

Annotation

Message Key

@Length

javax.faces.validator.LengthValidator.MINIMUM
javax.faces.validator.LengthValidator.MAXIMUM

@LongRange

javax.faces.validator.LongRangeValidator.NOT_IN_RANGE
javax.faces.validator.LongRangeValidator.MAXIMUM
javax.faces.validator.LongRangeValidator.MINIMUM
javax.faces.validator.LongRangeValidator.TYPE

@DoubleRange

javax.faces.validator.DoubleRangeValidator.NOT_IN_RANGE
javax.faces.validator.DoubleRangeValidator.MAXIMUM
javax.faces.validator.DoubleRangeValidator.MINIMUM
javax.faces.validator.DoubleRangeValidator.TYPE

JPA annotations

Annotation

Message Key

@Column

field_required
field_too_long

@Basic

field_required
field_too_long

@Id

field_required
field_too_long

@OneToOne

field_required
field_too_long

@ManyToOne

field_required
field_too_long

Bean-Validation Constraints

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.]

ExtVal Simple-Validation Annotations

Annotation

Message Key

@Required

field_required

@Pattern

no_match

ExtVal Cross-Validation Annotations

Annotation

Message Key

@Equals

duplicated_content_required

@NotEquals

duplicated_content_denied

@DateIs

wrong_date_not_before
wrong_date_not_after
wrong_date_not_equal

@RequiredIf

empty_field

@EmptyIf

field_not_empty

Define a central Resource-Bundle

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:

  1. Create a new resource bundle, according to the standards of Java (a properties files with the correct postfixes for the languages and countries). An example could be com/mycompany/myprog/messages.properties, com/mycompany/myprog/messages.properties, etc ..
  2. Define this resource bundle as message bundle in your faces-config file
    <application>
      <message-bundle>com.mycompany.myprog.messages</message-bundle>
    </application>
    
  1. Define the following web application parameters in your web.xml file.
    <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.

  • No labels