Bean Validation - Sign Up

Sign Up

Add Hibernate Validator dependency to pom.xml.

pom.xml
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
	<groupId>org.hibernate</groupId>
	<artifactId>hibernate-validator</artifactId>
	<version>6.0.16.Final</version>
</dependency>

Since the contents of pom.xml have changed, run Maven, Update Project Cofiguraion menu to synchronize with Eclipse.
Bean validation need <mvc:annotation-driven /> in spring-bbs-servlet.xml.

Declare a check rule on the bean that is bound to the sign up form.

User.java
import javax.validation.constraints.Email;
import javax.validation.constraints.Size;

@Size(min=5, message="It is not an email.")
@Email(message="It is not an email.")
private String email;

@Size(min=4, message="Your password must be at least 4 characters long.")
private String passwd;

@Size(min=2, message="The full name must exceed two characters.")
private String name;

@Size(min=6, max=20, message="It is not a mobile number.")
private String mobile;

The membership form handler must create an empty object with no content and pass it to the view.
This is a spec and should be followed.
Add bean validation logic to the request handler method that handles subscription.
In the parameter list of this method, you must declare a parameter of type BindingResult after the @Valid annotated parameter.
Otherwise, an HTTP 400 error will occur.

UsersController.java
import javax.validation.Valid;
import org.springframework.validation.BindingResult;

@RequestMapping(value="/signUp", method=RequestMethod.GET)
public String signUp(Model model) {
    model.addAttribute(WebContants.USER_KEY, new User());
    
    return "users/signUp";
}

@RequestMapping(value="/signUp", method=RequestMethod.POST)
public String signUp(@Valid User user, BindingResult bindingResult) {
    //A bean that fails validation is stored in the BindingResult and the BindingResult is passed to the view.
    if (bindingResult.hasErrors()) {
        return "users/signUp";
    }
	
    //...omit...
}

Modify signUp.jsp as shown below.(<sf:errors > is a Spring form tag that displays a verification error to the user)

signUp.jsp
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>

<sf:form id="signUpForm" action="signUp" method="post" modelAttribute="user" 
        onsubmit="return check();">
<sf:errors path="*" cssClass="error" />
<table>
<tr>
    <td style="width: 200px;">Full Name</td>
    <td style="width: 390px">
        <sf:input path="name" /><br />
        <sf:errors path="name" cssClass="error" />
    </td>
</tr>
<tr>
    <td>Password</td>
    <td>
        <sf:password path="passwd" /><br />
        <sf:errors path="passwd" cssClass="error" />
    </td>
</tr>
<tr>
    <td colspan="2" style="text-align: center;font-weight: bold;">
        Do not input email account password!
    </td>
</tr>
<tr>
    <td>Confirm</td>
    <td><input type="password" name="confirm" /></td>
</tr>
<tr>
    <td>Email</td>
    <td>
        <sf:input path="email" /><br />
        <sf:errors path="email" cssClass="error" />
    </td>
</tr>
<tr>
    <td>Mobile</td>
    <td>
        <sf:input path="mobile" /><br />
        <sf:errors path="mobile" cssClass="error" />
    </td>
</tr>
</table>
<div style="text-align: center;padding-bottom: 15px;">
    <input type="submit" value="Submit" />
</div>
</sf:form>

Add the following to stylesheet.

.error {
	color: red;
}

Test Screen

Sign Up
Bean validaion failed

References