2 Replies Latest reply on Sep 10, 2010 12:45 AM by harsh4u89

    hibernate validator in not working properly.

    harsh4u89

      I have made a form in which i have generated ENTITIES from data base from SEAM GENERATE ENTITIES .


      But when I use validation in the from and when I dont fill one of the fields ,it  doesn't show any validation message.


      Please suggest some solution


      Here is my form


      home.xhtml


      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
          xmlns:s="http://jboss.com/products/seam/taglib"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:rich="http://richfaces.org/rich"
           
           xmlns:a4j="https://ajax4jsf.dev.java.net"
           template="layout/template.xhtml">
           
          <ui:define name="body">
           
              <h:form id="register_student">
              <s:validateAll>
          <rich:panel>
              <f:facet name="header">WELCOME TO STUDENT REGISTRATION FACILITY</f:facet>
              <h:messages />
              
            
              <h:panelGrid columns="3" id="stdenroll" border="0" columnClasses="name;value;name;value">
                      <h:outputLabel for="fname">First Name *</h:outputLabel>
                      <h:inputText id="fname" size="40" value="#{userdata.fname}" >
                      <s:validate />
                      </h:inputText>
                       <h:message for="fname" />
                            
                      <h:outputLabel for="mname">Middle Name</h:outputLabel>
                      <h:inputText id="mname" size="40" value="#{userdata.mname}"/>  
                      <h:message for="mname" />
                      
                      <h:outputLabel for="lname">Last Name *</h:outputLabel>
                      <h:inputText id="lname" size="40"  value="#{userdata.lname}"/>
                      <h:message for="lname" />              
                                                       
                                      
                      <h:outputLabel for="allocaterollno">ROLLNO *</h:outputLabel>
                          <h:inputText id="allocaterollno" value="#{userdata.rollNo}" size="40">
                          <s:validate/>
                          </h:inputText>
                      <h:message for="allocaterollno" />      
                                                   
                         
                      <h:outputLabel for="fathername">Father's Name *</h:outputLabel>
                      <h:inputText id="fathername" size="40"   value="#{userdata.fathername}"/>
                      <h:message for="fathername" />
                                    
                        
                                  
                      <h:outputLabel for="mothername">Mother's Name *</h:outputLabel>
                      <h:inputText id="mothername" size="40" value="#{userdata.mothername}"/>
                      <h:message for="mothername" />
                                    
                      <h:outputLabel for="genderselect">Gender *</h:outputLabel>
                      <h:selectOneRadio id="genderselect" value="#{userdata.gender}"   >
                             <f:selectItem itemValue="M" itemLabel="M"  /> 
                             <f:selectItem itemValue="F" itemLabel="F"/>
                           </h:selectOneRadio>
                           <h:message for="genderselect" />
                           
                                  
                                                           
                          <h:outputLabel for="dob">D.O.B. *</h:outputLabel>
                      <rich:calendar id="dob" value="#{userdata.dob}" >           </rich:calendar>
                          <h:message for="dob" />            
                                       
                                       
                                                               
                         <h:outputLabel for="em">E-Mail *</h:outputLabel>  
                          
                          
                                 
                                   <h:inputText value="#{userdata.email}"  id="em" size="40">
                      
                                     <s:validate />
                                        </h:inputText>
                                      
                               
                     <h:message for="em" />
                          
                           
                                                    
                      <h:outputLabel for="categor">Category *</h:outputLabel>  
                          <h:selectOneMenu id="categor" value="#{userdata.category}"  noSelectionLabel=" -- Select --">
                                       <f:selectItem itemValue="Gen" itemLabel="Gen"/>
                                       <f:selectItem itemValue="OBC" itemLabel="OBC"/>
                                       <f:selectItem itemValue="SC" itemLabel="SC"/>
                                       <f:selectItem itemValue="ST" itemLabel="ST"/>
                                       <f:selectItem itemValue="PH" itemLabel="PH"/>
                                      
                          </h:selectOneMenu>
                          <h:message for="categor" />
                          
                          
                                      
                         <h:outputLabel for="branch">Branch *</h:outputLabel>  
                          <h:selectOneMenu id="branch" value="#{userdata.branch}" noSelectionLabel=" -- Select --">
                          <f:selectItem itemValue="Computer" itemLabel="Computer"/>
                          <f:selectItem itemValue="IT" itemLabel="IT"/>
                          <f:selectItem itemValue="ECE" itemLabel="ECE"/>
                          <f:selectItem itemValue="Mechanical" itemLabel="Mechanical"/>
                          <f:selectItem itemValue="Electrical" itemLabel="Electrical"/>
                          <f:selectItem itemValue="Civil" itemLabel="Civil"/>
                          <f:selectItem itemValue="IEM" itemLabel="IEM"/>
                                      
                          </h:selectOneMenu>
                          <h:message for="branch" />           
                                  
                      <h:outputLabel for="section">Section *</h:outputLabel>  
                          <h:inputText id="section" value="#{userdata.section}" />
                          <h:message for="section" />
                          
                           
                          <h:outputLabel for="phoneno" >Phone No. *</h:outputLabel>
                          <h:inputText id="phoneno" value="#{userdata.phonenum}" />
                           
                           
                           <h:message for="phoneno" />
                          
                     
                                         
                                         
                          <h:outputLabel for="address" >Address *</h:outputLabel>
                          <h:inputText id="address" value="#{userdata.address}" />
                          <h:message for="address" />
                          
                               
                          <h:outputLabel for="city" >City *</h:outputLabel>
                          <h:inputText id="city" value="#{userdata.city}" />     
                          <h:message for="city" />            
                           
                            
                          <h:outputLabel for="state" >State *</h:outputLabel>
                          <h:inputText id="state" value="#{userdata.state}" />
                          <h:message for="state" />            
                                      
                          <h:outputLabel for="pincode" >Pincode</h:outputLabel>
                          <h:inputText id="pincode" value="#{userdata.pincode}" />
                          <h:message for="pincode" />
                                       
                        <h:commandButton id="hello" label="Proceed" value="Proceed"
                          action="#{registration.doRegister()}" >
                          <s:conversationPropagation type="join"  />
                      </h:commandButton>
               </h:panelGrid>
              </rich:panel>
           </s:validateAll>
             </h:form>
          
          </ui:define>
      </ui:composition>
      
      



      and here is my entity bean



      UserData.java


      package org.domain.reg.entity;
      
      // Generated Sep 8, 2010 12:36:40 AM by Hibernate Tools 3.3.0.GA
      
      import java.util.Date;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      
      import org.hibernate.validator.Email;
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      
      /**
       * UserData generated by hbm2java
       */
      @Name("userdata")
      @Entity
      @Table(name = "user_data", catalog = "onlyregistration")
      public class UserData implements java.io.Serializable {
      
           /**
            * 
            */
           private static final long serialVersionUID = 1L;
           private int rollNo;
           private String email;
           private String fname;
           private String mname;
           private String lname;
           private Date dob;
           private String gender;
           private String branch;
           private String section;
           private String fathername;
           private String mothername;
           private String category;
           private String address;
           private String city;
           private String state;
           private Integer pincode;
           private String phonenum;
      
           public UserData() {
           }
      
           public UserData(int rollNo, String email, String fname, String lname,
                     Date dob, String gender, String branch, String section,
                     String fathername, String mothername, String category,
                     String address, String city, String state, String phonenum) {
                this.rollNo = rollNo;
                this.email = email;
                this.fname = fname;
                this.lname = lname;
                this.dob = dob;
                this.gender = gender;
                this.branch = branch;
                this.section = section;
                this.fathername = fathername;
                this.mothername = mothername;
                this.category = category;
                this.address = address;
                this.city = city;
                this.state = state;
                this.phonenum = phonenum;
           }
      
           public UserData(int rollNo, String email, String fname, String mname,
                     String lname, Date dob, String gender, String branch,
                     String section, String fathername, String mothername,
                     String category, String address, String city, String state,
                     Integer pincode, String phonenum) {
                this.rollNo = rollNo;
                this.email = email;
                this.fname = fname;
                this.mname = mname;
                this.lname = lname;
                this.dob = dob;
                this.gender = gender;
                this.branch = branch;
                this.section = section;
                this.fathername = fathername;
                this.mothername = mothername;
                this.category = category;
                this.address = address;
                this.city = city;
                this.state = state;
                this.pincode = pincode;
                this.phonenum = phonenum;
           }
      
           @Id
           @Column(name = "RollNo", unique = true, nullable = false)
           public int getRollNo() {
                return this.rollNo;
           }
      
           public void setRollNo(int rollNo) {
                this.rollNo = rollNo;
           }
           @Email
           @Column(name = "Email", nullable = false, length = 40)
           @NotNull
           @Length(max = 40)
           public String getEmail() {
                return this.email;
           }
      
           public void setEmail(String email) {
                this.email = email;
           }
      
           @Column(name = "fname", nullable = false, length = 20)
           @NotNull
           @Length(max = 20)
           public String getFname() {
                return this.fname;
           }
      
           public void setFname(String fname) {
                this.fname = fname;
           }
      
           @Column(name = "mname", length = 20)
           @Length(max = 20)
           public String getMname() {
                return this.mname;
           }
      
           public void setMname(String mname) {
                this.mname = mname;
           }
      
           @Column(name = "lname", nullable = false, length = 20)
           @NotNull
           @Length(max = 20)
           public String getLname() {
                return this.lname;
           }
      
           public void setLname(String lname) {
                this.lname = lname;
           }
      
           @Temporal(TemporalType.TIME)
           @Column(name = "dob", nullable = false, length = 8)
           @NotNull
           public Date getDob() {
                return this.dob;
           }
      
           public void setDob(Date dob) {
                this.dob = dob;
           }
      
           @Column(name = "gender", nullable = false, length = 2)
           @NotNull
           @Length(max = 2)
           public String getGender() {
                return this.gender;
           }
      
           public void setGender(String gender) {
                this.gender = gender;
           }
      
           @Column(name = "branch", nullable = false, length = 20)
           @NotNull
           @Length(max = 20)
           public String getBranch() {
                return this.branch;
           }
      
           public void setBranch(String branch) {
                this.branch = branch;
           }
      
           @Column(name = "section", nullable = false, length = 10)
           @NotNull
           @Length(max = 10)
           public String getSection() {
                return this.section;
           }
      
           public void setSection(String section) {
                this.section = section;
           }
      
           @Column(name = "Fathername", nullable = false, length = 40)
           @NotNull
           @Length(max = 40)
           public String getFathername() {
                return this.fathername;
           }
      
           public void setFathername(String fathername) {
                this.fathername = fathername;
           }
      
           @Column(name = "Mothername", nullable = false, length = 40)
           @NotNull
           @Length(max = 40)
           public String getMothername() {
                return this.mothername;
           }
      
           public void setMothername(String mothername) {
                this.mothername = mothername;
           }
      
           @Column(name = "Category", nullable = false, length = 5)
           @NotNull
           @Length(max = 5)
           public String getCategory() {
                return this.category;
           }
      
           public void setCategory(String category) {
                this.category = category;
           }
      
           @Column(name = "address", nullable = false, length = 50)
           @NotNull
           @Length(max = 50)
           public String getAddress() {
                return this.address;
           }
      
           public void setAddress(String address) {
                this.address = address;
           }
      
           @Column(name = "city", nullable = false, length = 20)
           @NotNull
           @Length(max = 20)
           public String getCity() {
                return this.city;
           }
      
           public void setCity(String city) {
                this.city = city;
           }
      
           @Column(name = "state", nullable = false, length = 40)
           @NotNull
           @Length(max = 40)
           public String getState() {
                return this.state;
           }
      
           public void setState(String state) {
                this.state = state;
           }
      
           @Column(name = "pincode")
           public Integer getPincode() {
                return this.pincode;
           }
      
           public void setPincode(Integer pincode) {
                this.pincode = pincode;
           }
      
           @Column(name = "phonenum", nullable = false, length = 20)
           @NotNull
           @Length(max = 20)
           public String getPhonenum() {
                return this.phonenum;
           }
      
           public void setPhonenum(String phonenum) {
                this.phonenum = phonenum;
           }
      
      }
      
      



      When any of the field is blank ,like First name, no messages are generated and the value is saved in database with null value. I am using mysql5

        • 1. Re: hibernate validator in not working properly.
          njrich28

          For every field that is required you must specify required\="true" on the input element eg


                          <h:inputText id="fname" size="40" value="#{userdata.fname}" required="true" >
                          <s:validate />
                          </h:inputText>
          



          This is a 'feature' of JSF (and not a problem with Hibernate Validator). If you leave a field blank then JSF completed disregards it (ie it doesn't call any converters for it and, importantly, doesn't call any validators for it). Since it doesn't call any validators then Hibernate Validator can't validate it. Empty fields are always assumed by JSF to be valid (hence it doesn't call any validators).


          The required\="true" attribute is processed by JSF during the 'Apply Request Values' phase before any converters or validators are called.

          • 2. Re: hibernate validator in not working properly.
            harsh4u89

            Thank you very much...


            I did the same and the form is working


            Regads