Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does it work with bean validation messages and i18n in JSF2?

I created validation messages(ValidationMessages.properties) file to make the i18n possible in my project.

It looks like:

pwtNumber.error=PWT Number error.. 

I defined it in faces-config.xml:

<message-bundle>com.mycompany.web.i18n.ValidationMessages</message-bundle>

In my code I used it in this way:

 @Null(message = "{pwtNumber.error}")
    public String getPwtNummer() {
        return pwtNummer;
    }

But the problem is i don't get the error message but the key from the properties file. It is the error message that i get:

myForm:pwtNummer: {pwtNumber.error}

How can i solve it?

like image 355
Kayser Avatar asked Sep 05 '12 11:09

Kayser


People also ask

How can you explain Bean Validation?

JavaBeans Validation (Bean Validation) is a new validation model available as part of Java EE 6 platform. The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean. Constraints can be built in or user defined.

How does Java Bean validation work?

Java Bean Validation, also known as JSR-303 (Java Specification Requests), is an annotation based validation specification for validating fields on objects. Java Bean Validation uses flexible annotations, which can be overridden or extended through the Java code or in an XML file, to apply validation constraints.

What is JPA validator?

Data validation is a common task that occurs in all layers of an application, including persistence. The Java™ Persistence API (JPA) 2.0 provides support for the Bean Validation API so that data validation can be done at run time.


1 Answers

You're confusing JSF builtin validation with JSR303 bean validation.

The <message-bundle> is to be used to override/specify JSF builtin validation messages, not JSR303 bean validation messages. Although you used the right filename for the JSR303 bean validation bundle file, ValidationMessages.properties, the JSR303 bean validation however also requires the bundle file to be placed in the root of the classpath (thus, without any package; in the default package). After you've fixed that, don't forget to remove the incorrect <message-bundle> entry.

See also:

  • Internationalization in JSF, when to use message-bundle and resource-bundle?
  • Localization with bean validation in JSF
like image 133
BalusC Avatar answered Oct 24 '22 13:10

BalusC