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:
This is the startSignup.jsp 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 myproject.web.pojo.User POJO 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>
(note: as you can see, when I work with Spring Validation, the form validates successfully)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()); // } }
And finally, this is my SignupAction code:
When I work with Hibernate Validation, the flow arrives to the saveUser method, without any validation.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; } }
What I'm doing wrong?
Thanks a lot for helping me!![]()


Reply With Quote
