Bean Validation is part of the Java EE 7 specification. Some usage is described   in my book Web Development with Java and JSF, [1] and a full description of the Bean Validation is available at [2].

Bean Validation might be used for validation in JSF too. Behind the scenes, there is a lot to do: JSF must discover whether Bean Validation is used and invoke it if needed. The essence of this invocation is shown in a short code snippet which demonstrates the principle.

@Inject private AccountRequest _accountRequest;

Validator validator = Validation.buildDefaultValidatorFactory()
                                                               .getValidator();
Set<ConstraintViolation<AccountRequest>> violations = 
                                                                         validator.validate(_accountRequest);

This snippet only shows how to check the violations and assign them to a set. What JSF performs behind the scenes is much more than this.

If JSF discovers one or more   constraint violations, it will set the appropriate messages and re-render the current page to display them. The data is not transferred to the model.

Bean Validation offers an option to annotate a customized message.

@Size(min = 3, max = 50,
   message = "Length of first name must be between {min} and {max} characters")

Often you want to develop an international application. By the approach shown here, it is possible to customize the message, but not to localize it. This can be realized by creating a ValidationMessages.properties and its localized variants (e.g. ValidationMessages_de.properties) in the default package. Within this file, you may override the default values or create your own customized messages including their localized variants.

Step by step tutorial to create a customize message

  1. (optional) In the project tree, select the Other Sources/src/main/resources folder.
  2. To create such a properties file using NetBeans, choose New File (e.g. by pressing Ctrl+N).

    410_CreatePropertiesFile

  3. In the dialog, choose category Other and then Properties File.
  4. Click onto Next
  5. For name enter ValidationMessages
  6. Due to step 1., the folder should be pre-populated with src\main\resources. Otherwise enter this value or browse to the desired folder.
  7. Click Finish.
    NetBeans creates the properties file for you.
  8. Right click the properties file and choose Customize.
    NetBeans opens the customize dialog.

    410_CustomizeProperties

  9. Add locales of your choice
  10. Right click any of these new files and choose open (not edit).
    NetBeans opens the properties editor with all locales.
  11. Click onto New Property….
  12. For Key enter javax.validation.constraints.Size.message.
  13. For value enter your customize message, e.g. `The length must be between {min} and {max} characters.`
  14. Run the app and enter a name which is either too short or too long
    JSF displays the customized message

Details of customized messages

So how can you determine that magic key? And how to create other customized messages for the same constraint (on other fields or getters)? You’ll find the answer in this book [1] – or by browsing the Java source code.
Using NetBeans, place the cursor onto the @Size annotation and invoke Ctrl+B (go to declaration). NetBeans will open Size.java in its editor.

[...]
public @interface Size {
[...]
public String message()
default "{javax.validation.constraints.Size.message}";

public int min() default 0;

public int max() default 2147483647;
[...]
}

Here you’ll find the declaration of message with a default value. And take a look at the properties min and max. Both are public and might be referenced in the value of the message if placed into the message within curly braces.

With that knowledge, we can create different keys at different locations.

@Size(min = 3, max = 50, message = "{validation.lastname.size}")
public String getLastName() {
  return _lastName;
}

Watch out the curly braces which indicate the key. Without them, the message would be treated as plain text. The curly braces are not part of the key.
Within the ValidationMessages.properties we now can add the key validation.lastname.size and a value like The length of LastName must be between {min} and {max} characters.

[1] leanpub.com/jsf
[2] beanvalidation.org