4 Replies Latest reply on Mar 12, 2012 7:04 AM by jonathan.man

    Validation annotation working on getter but not on property

    jonathan.man

      Hi everyone.

       

      I'm experimenting with JSF2, and wanted to give a shot at validation by annotation.

      I can make it work by annotatiing the getter, but not the property.

      Examples on the net all works with property annotations.

      I'm curious as to why I can't get it to work.

      I'm using hibernate validator.

       

      Here is my setup :

      Eclipse Indigo 3.7.2

      jdk1.7

       

       

      My Pom :

       

      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

          <modelVersion>4.0.0</modelVersion>

          <groupId>test</groupId>

          <artifactId>validation</artifactId>

          <packaging>war</packaging>

          <version>0.0.1-SNAPSHOT</version>

          <name>validation Maven Webapp</name>

          <url>http://maven.apache.org</url>

          <properties>

              <junit.version>4.10</junit.version>

              <jmock.version>2.5.1</jmock.version>

              <mojarra.version>2.1.7</mojarra.version>

              <richfaces.version>4.2.0.Final</richfaces.version>

              <spring.version>3.1.1.RELEASE</spring.version>

              <hibernate-validator.version>4.2.0.Final</hibernate-validator.version>

              <el.version>2.2</el.version>

          </properties>

          <dependencies>

              <dependency>

                  <groupId>com.sun.faces</groupId>

                  <artifactId>jsf-api</artifactId>

                  <version>${mojarra.version}</version><!--$NO-MVN-MAN-VER$ -->

              </dependency>

              <dependency>

                  <groupId>com.sun.faces</groupId>

                  <artifactId>jsf-impl</artifactId>

                  <version>${mojarra.version}</version><!--$NO-MVN-MAN-VER$ -->

              </dependency>

              <dependency>

                  <groupId>org.hibernate</groupId>

                  <artifactId>hibernate-validator</artifactId>

                  <version>${hibernate-validator.version}</version>

              </dependency>

          </dependencies>

          <build>

              <finalName>validation</finalName>

              <plugins>

                  <plugin>

                      <groupId>org.apache.maven.plugins</groupId>

                      <artifactId>maven-compiler-plugin</artifactId>

                      <version>2.0.2</version>

                      <configuration>

                          <source>1.7</source>

                          <target>1.7</target>

                          <encoding>UTF-8</encoding>

                      </configuration>

                  </plugin>

              </plugins>

          </build>

      </project>

       

       

      My Sample page :

       

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml"

          xmlns:f="http://java.sun.com/jsf/core"

          xmlns:h="http://java.sun.com/jsf/html"

          xmlns:ui="http://java.sun.com/jsf/facelets">

      <h:head />

      <h:body>

          <h:form>

              <h:panelGrid columns="3">

                  <h:outputText value="Name :" />

                  <h:inputText id="inputName"

                               value="#{someBean.name}">

                  </h:inputText>

                  <h:message for="inputName" id="mess1" />

       

                  <h:outputText value="Surname" />

                  <h:inputText id="inputSurname"

                              required="true"

                              requiredMessage="Name mandatory" value="#{someBean.surname}"

                              validatorMessage="Invalid surname">

                      <f:validateLength minimum="5" maximum="10" />

                  </h:inputText>

                  <h:message for="inputSurname" id="mess2" />

       

                  <h:commandButton value="Do Something" actionListener="#{someBean.doSomething}" />

              </h:panelGrid>

              <h:messages id="allMess" />

          </h:form>

      </h:body>

      </html>

       

       

      And my sample bean :

       

      package test.validation;

       

      import javax.faces.bean.ManagedBean;

      import javax.faces.bean.RequestScoped;

      import javax.validation.constraints.NotNull;

      import javax.validation.constraints.Size;

       

      @ManagedBean

      @RequestScoped

      public class SomeBean {

         

          @NotNull(message="Name Mandatory")

          @Size(min=5, max=10, message="Invalid name")

          private String fName = null;

          //@Size(min=5, max=10, message="Invalid surname")

          private String fSurname = null;

         

          public void doSomething() {

              System.out.println("Doing something - this = " + this.toString());

              System.out.println("Doing something - " + this.fName + "/" + this.fSurname);

          }

         

          public String getName() {

              return this.fName;

          }

         

          public void setName(String pName) {

              this.fName = pName;

          }

          public String getSurname() {

              return this.fSurname;

          }

          public void setSurname(String pSurname) {

              this.fSurname = pSurname;

          }

      }

       

      Any idea?