29

I am using Hibernate Validation annotations in my JSF managed bean. When I use @NotNull, @NotBlank or @NotEmpty they doesn't seem to be triggered in any way.

@NotBlank(message = "{name.required}")
public String name;

View:

<h:outputLabel value="Name:" /> 
<h:inputText id="name" value="#{person.name}" size="20" />
<h:message for="name" style="color:red" />

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

4 Answers4

68

Introduction

Since you didn't give any feedback on my comment with the question what container you're using, I peeked around in your question history to learn what containers you're all using. As far I found only Tomcat. So I'll for this answer assume that you're indeed using Tomcat as I initially guessed while posting the comment.

Make sure you install all JARs

Tomcat does not ship with any JSR303 Bean Validation API/implementation out the box. You need to download and install it yourself. That you got those annotations to compile means that you've correctly dropped the hibernate-validator.jar file (naming may differ per version) in /WEB-INF/lib folder of your webapp. That those annotations in turn does not seem to work in any way can only mean that you didn't read the readme.txt and/or forgot to add the JARs from the /lib/required folder of the Hibernate Validator library zip/tgz file: slf4j-api.jar and validation-api.jar. The last one is mandatory in order to get the annotations to actually work. So, to get Hibernate Validator to work in Tomcat, you need the following JARs in webapp's /WEB-INF/lib:

  • validation-api.jar (contains the abstract API and the annotation scanner)
  • hibernate-validator.jar (contains the concrete implementation)
  • slf4j-api.jar (just to get its logger to work as well)

This way @NotBlank and @NotEmpty must work. The @NotNull deserves special attention; empty input fields are namely by default received as empty strings from the client (webbrowser) due to the nature of HTTP request parameters. Empty strings are not the same as null, so the @NotNull will by default never kick in. JSF is however configureable to interpret them as null by just adding the following context parameter to web.xml:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

This way the @NotNull must work as well.

BV works but only empty fields not

If it still doesn't work (i.e. none of the 3 annotations work, but others like @Size(min=5) for a minimum length of 5 works fine), then chances are big that you've the following context parameter in web.xml as well:

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
</context-param>

You should then remove it (it defaults to auto, i.e. only when JSR303 Bean Validation API is found in runtime classpath) or to set it to true.

BV does not work at all

When actually nothing of BV works, also not @Size, @Pattern, etc, then you should verify if you do not have the following in your form:

<f:validateBean disabled="true" />

You should then remove it (it will just by default kick in) or to set disabled="false".

Make sure you use most recent Mojarra

When BV still doesn't work, then verify if you're not using an old Mojarra version between 2.2.3 and 2.2.6. Those versions had a classloading delegate bug causing Bean Validation on Tomcat and clones to be completely invisible. This is reported as Mojarra issue 3183 and fixed in Mojarra 2.2.7.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 4
    hi BalusC, sorry i didn't see your comment on my post but now, thanks a lot for detailed explanation, the problem was that i am having: javax.faces.VALIDATE_EMPTY_FIELDS false i didn't saw it, i just used the web.xml from an online sample, and i am new to jsf, thanks a lot. – Mahmoud Saleh Sep 26 '11 at 09:28
  • @BalusC, I have all required dependencies and on the same (String) field `@NotEmpty` is invoked and works as expected, but `@NotBlank` is ignored. If annotated with `@NotBlank` it does not generate any error even for null and empty strings. Do you know what may cause the problem? – Dima Jul 19 '12 at 11:04
  • 9
    Like solving a murder case. – Markos Fragkakis Dec 11 '12 at 15:58
  • "Since you didn't give any feedback on my comment with the question what container you're using, I peeked around in your question history to learn what containers you're all using. As far I found only Tomcat. So I'll for this answer assume that you're indeed using Tomcat as I initially guessed while posting the comment." wow...just...wow... – arg20 Oct 09 '13 at 12:50
  • I do not have access to all of my validation messages. @ NotEmpty in the same superclass is going well, whereas @ NotNull is only showing the default message. @BalusC or anyone got an idea? – marcel Nov 19 '14 at 14:24
  • great explanation ,i was using old mojara ... – ritu mansata Apr 28 '22 at 07:00
4

I had a similar issue and i was able to overcome the problem by including the below three jars in web-inf lib. Just add the hibernate validator jar and the required jars as provided in the zip file:

  • hibernate-validator-4.3.0.Final.jar
  • jboss-logging-3.1.0.CR2.jar
  • validation-api-1.0.0.GA.jar
Grzegorz Rożniecki
  • 27,415
  • 11
  • 90
  • 112
abhi
  • 101
  • 1
  • 1
0

add

<context-param>
        <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>

in web.xml

and add

<dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.23.Final</version>
        </dependency>
    

    <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-api</artifactId>
                <version>2.2.13</version>
            </dependency>
    
    
            <dependency>
                <groupId>com.sun.faces</groupId>
                <artifactId>jsf-impl</artifactId>
                <version>2.2.13</version>
            </dependency> 

in pom.xml

ritu mansata
  • 77
  • 2
  • 13
-3

If it still fails to validate even after adding the above indicated jars, then you might have missed adding tag

 <mvc:annotation-driven />

in the spring configuration file.

sarayu
  • 1
  • 1