1

I couldn't get Java Bean Validation (JSR-303) working. It doesn't give any error and processes the action method. I put one character to "name" inputText, it accepts and process() method gets executed (Size annotation doesn't apply).

When I put one character I expect the My.Process() method to not to be executed, am I not right? Is there any configuration I overlook to get validation working?

In WEB-INF/lib dir of my project I have validation-api-1.0.0.GA.jar, hibernate-validator-4.2.0.Final.jar.
App Server: Tomcat 7.0.25 (inside Tomcat's Lib folder I have myfaces-api-2.1.5.jar and myfaces-impl-2.1.5.jar).
IDE: Eclipse Helios SR2

I get the message "INFO: MyFaces Bean Validation support disabled" from console, but I have the jars as I specified.

Bean

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.validation.constraints.Size;

@ManagedBean(name="my")
@RequestScoped
public class MyBean implements Serializable {
  @Size(min=3, message="ERROR")
  private String name;

  public String process() {...}

  ...getters, setters, etc...
}

JSF form

<h:form>
    <h:messages />
    <h:inputText value="#{my.name}" />
    <h:commandButton value="Save" action="#{my.process}" />
</h:form>

I also tried:
@NotEmpty, @NotBlank,
@org.hibernate.validator.constraints.Length(min=3, message="ERROR")
but they don't work too.

Seregwethrin
  • 1,319
  • 2
  • 13
  • 24
  • 2
    Your code works in my environment (GF 3.1.1, Mojarra 2.1.3). I have no additional validation apis other than the one with Glassfish (Java EE6 bean-validation.jar). – Matt Handy Mar 12 '12 at 15:20
  • Well I actually simplified my code before writing here but this exact simplified version doesn't work for me too on my environment that I specified above. I'm really tired of trying to find the problem. It doesn't make any sense? I read dozens of web pages about jsf bean validation and according to all of them it should work... – Seregwethrin Mar 12 '12 at 15:35
  • I get the message below at console, I'm trying to find why it says this. I have the jars I specified above in place. "INFO: MyFaces Bean Validation support disabled" – Seregwethrin Mar 12 '12 at 16:14
  • Haha it worked! I downloaded "bean-validator.jar" from some place and put it into my classpath and now bean validation enabled, also it works :) Thank you for pointing out that jar file Matt! But it is really weird that there's no information related bean validation to that jar. I can't even find a proper information about which project that jar belongs to. – Seregwethrin Mar 12 '12 at 16:31
  • I investigated bean-validator.jar and it looks like a combination of validation-api.jar and hibernate-validator-4.0.2-GA.jar So I removed it and the validation now still works! I'm sometimes afraid of never understanding java totally :) – Seregwethrin Mar 12 '12 at 16:49
  • I could solve the same problem using maven by adding `javax.validationvalidation-api` to my pom.xml – devsnd Mar 21 '12 at 16:45

2 Answers2

3

I'd like to write a possible problem.

When I had have this variable

private String Name;

Validation didn't work because the getter and setters are like this

private String getName(){}

So I renamed Name to

private String name;

and it started to validate :)

Probably that was the problem.

Seregwethrin
  • 1,319
  • 2
  • 13
  • 24
  • See also http://stackoverflow.com/questions/11074802/bean-validation-not-working-if-name-variable-is-prefixed-with-underscore – Robert Dec 06 '13 at 19:19
1

Thanks for this! FYI, maven coordinates for bean-validator:

<dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>bean-validator</artifactId>
        <version>3.0-JBoss-4.0.2_03</version>
</dependency>
Domenic D.
  • 5,276
  • 4
  • 30
  • 41