1 Reply Latest reply on Nov 1, 2008 3:39 AM by joblini

    Hibernate Validators not fired

    nbhatia.bhatian.comcast.net

      I am trying to mimick the SeamSpace example to do some hibernate validator validations. Unfortunately the validations are not kicking in and I am able to save invalid data to the database. Any ideas where to look for problems?


      Here's an example of a Person entity in my application with validations on firstName and lastName (Note that I had to specify @Column(nullable=false) to make the database column non null - I would have thought that specifying @NotNull would be enough. Does anyone have an explanation for that?):


      @Entity
      public class Person {
          private String firstName;
          private String lastName;
          
          @NotNull
          @Length(min = 3, max = 40)
          @Pattern(regex="[a-zA-Z]+", message="First name must only contain letters")
          @Column(nullable=false)
          public String getFirstName() {
              return firstName;
          }
          public void setFirstName(String firstName) {
              this.firstName = firstName;
          }
      
          @NotNull
          @Length(min = 3, max = 40)
          @Pattern(regex="[a-zA-Z]+", message="Last name must only contain letters")
          @Column(nullable=false)
          public String getLastName() {
              return lastName;
          }
          public void setLastName(String lastName) {
              this.lastName = lastName;
          }
      }
      



      Here's the front-end code that sets the attributes of the Person object:


      <h:form styleClass="register">
           <s:validateAll>
              <div class="formRow">
                  <h:outputLabel for="firstName">
                      First Name<em>*</em>
                  </h:outputLabel>
                  <h:inputText
                      id="firstName"
                      value="#{register.newPerson.firstName}"
                      required="true" />
                  <div class="validationError"><h:message for="firstName" />
                  </div>
              </div>
              ...
           </s:validateAll>
      
           <div class="buttons"><h:commandButton value="Register"
                action="#{register.register}" styleClass="registerButton" /></div>
      
      </h:form>
      



      Here's the code that persists a new Person in my RegisterAction class:


      public class RegisterAction {
          ...
          private Person newPerson = new Person();
      
          public void register() {
              ...
              entityManager.persist(newPerson);      
              ...
          }
      }