Announcement Announcement Module
Collapse
No announcement yet.
Problems with Hibernate Validator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problems with Hibernate Validator

    Hi,

    I'm trying to validate a form using Spring WebFlow and Hibernate Validator Annotations, but I'm doing something wrong.

    If I use the Spring validation way (ValidationContext, MessageContext, validate${state}, etc), all works fine, but I want to use the Hibernate Annotations like @Email, @NotEmpty, etc.

    I've read a lot of forums, Spring documentation, but I don't know what I'm doing wrong.

    At this moment, this is my view-state and next action code:

    Code:
    	<var name="user" class="myproject.web.pojo.User"/>
    
      	<view-state id="startSignup" model="user">
    	    <binder>
    	        <binding property="email" />
    	        <binding property="name" />
    	        <binding property="lastName" />
    	        <binding property="password" />
    	    </binder>
     		<on-entry>
     			<set name="flowScope.user" value="new info.teaming.web.pojo.User()" />
     		</on-entry>
     		<transition on="dataEntered" to="lookupUser" />
    	</view-state>
    
    	<action-state id="lookupUser">
    		<evaluate expression="signupActions.lookupUser(user)" />
    		<transition to="startSignup"
    on-exception="myproject.web.service.exception.UserAlreadyExistsException" />
    		<transition to="saveOrder" />
    	</action-state>
            (...)
    This is the startSignup.jsp code:

    Code:
    <!--  sf:form method="POST" modelAttribute="user" -->
    <sf:form method="POST" commandName="user" >
    	<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}" />
    	<fieldset>
    		<table cellspacing="0">
    			<tr>
    				<th>
    					<label for="name"><fmt:message key="signUp.name"/></label>
    				</th>
    				<td>
    					<sf:input path="name" size="25" id="name" />
    				</td>
    				<sf:errors path="name" />
    			</tr>
    			<tr>
    				<th>
    					<label for="lastName"><fmt:message key="signUp.lastName"/></label>
    				</th>
    				<td>
    					<sf:input path="lastName" size="45" maxlength="45" id="lastName" />
    				</td>
    			</tr>
    			<tr>
    				<th>
    					<label for="password"><fmt:message key="signUp.password"/></label>
    				</th>
    				<td>
    					<sf:password path="password" size="20" showPassword="true" id="password" />
    				</td>
    			</tr>
    			<tr>
    				<th>
    					<label for="email"><fmt:message key="signUp.email"/></label>
    				</th>
    				<td>
    					<sf:input path="email" size="30" id="email" />
    				</td>
    			</tr>
    			<tr>
    				<td>
    					<input type="submit" name="_eventId_dataEntered" value="<fmt:message key="signUp.register"/>"/>
    				</td>
    			</tr>
    		</table>
    	</fieldset>
    </sf:form>
    This is the myproject.web.pojo.User POJO code:

    Code:
    import java.io.Serializable;
    
    import org.hibernate.validator.constraints.Email;
    import org.hibernate.validator.constraints.Length;
    import org.hibernate.validator.constraints.NotEmpty;
    
    public class User implements Serializable {
    
    	private static final long serialVersionUID = 1L;
    
    	@NotEmpty
        @Length(min = 2, max = 25)  
        private String name;  
          
        @NotEmpty
        @Email
        private String email;  
          
        @NotEmpty
        @Length(min = 2, max = 45)  
        private String lastName;  
    
        @NotEmpty
        @Length(min = 6, max = 20)
        private String password;
        
        public String getName() {  
            return name;  
        }  
      
        public void setName(String name) {  
            this.name = name;  
        }  
          
        public String getEmail() {  
            return email;  
        }  
      
        public void setEmail(String email) {  
            this.email = email;  
        }  
      
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    
    	public String getLastName() {
    		return lastName;
    	}  
    	
    //	public void validateStartSignup(ValidationContext context) {
    //		MessageContext messages = context.getMessageContext();
    //		
    //		messages.addMessage(new MessageBuilder().error().source("name").code("user.name.notEmpty").build());
    //	}
    	
    }
    (note: as you can see, when I work with Spring Validation, the form validates successfully)

    And finally, this is my SignupAction code:

    Code:
    import myproject.web.model.UsrUser;
    import myproject.web.model.dao.UsrUserDAO;
    import myproject.web.pojo.User;
    import myproject.web.service.exception.UserAlreadyExistsException;
    
    import javax.annotation.Resource;
    import javax.validation.Valid;
    
    import org.springframework.stereotype.Component;
    import org.springframework.webflow.action.MultiAction;
    import org.springframework.webflow.execution.Event;
    
    @Component
    public class SignupActions extends MultiAction {
    
    	@Resource
    	UsrUserDAO usrUserDAO;
    	
    	private static final long serialVersionUID = 1L;
    	
    	public Event lookupUser(@Valid User user) throws UserAlreadyExistsException {
    		try {
    			usrUserDAO.findByEmail(user.getEmail());
    		} catch (javax.persistence.NoResultException e) {
    			return success();
    		}
    		
    		throw new UserAlreadyExistsException();
    	}
    
    	public void saveUser(UsrUser user) {
    		return;
    	}
    
    }
    When I work with Hibernate Validation, the flow arrives to the saveUser method, without any validation.

    What I'm doing wrong?

    Thanks a lot for helping me!

  • #2
    Oh! I forgot to tell you that I'm using Spring 3!!!

    Spring 3.0.5.RELEASE
    Spring WebFlow 2.2.1.RELEASE
    Hibernate Validator 4.0.2.GA

    Sorry about that!

    Comment


    • #3
      Problems with Hibernate Validator

      Hi,

      I am using an iMac and Windows XP on Boot Camp/parallels. I have been using Firefox for Connect meeting on both systems for several months and have not experienced problems. I dont however, have a lot of people attending meetings - rarely more than 4 at a time. I doubt that Firefox is your problem.

      I did have some web site issues when using Safari - not with Connect however. It too seemed to function smoothly for Connect participation. I have the plug-in installed for both systems.

      Rand Nelson

      Comment

      Working...
      X