Skip to content

Commit 323ee99

Browse files
committed
spring integration with hibernate validator
1 parent 9cd2c49 commit 323ee99

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

src/main/java/com/devjav/controller/AddEmployeeController.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313
import org.springframework.stereotype.Controller;
1414
import org.springframework.ui.Model;
1515
import org.springframework.validation.BindingResult;
16-
import org.springframework.web.bind.WebDataBinder;
17-
import org.springframework.web.bind.annotation.InitBinder;
1816
import org.springframework.web.bind.annotation.ModelAttribute;
1917
import org.springframework.web.bind.annotation.RequestMapping;
2018
import org.springframework.web.bind.annotation.RequestMethod;
2119

2220
import com.devjav.domain.Employee;
2321
import com.devjav.form.EmployeeForm;
2422
import com.devjav.service.EmployeeService;
25-
import com.devjav.validator.EmployeeValidator;
2623

2724
/**
2825
*
@@ -76,9 +73,4 @@ public EmployeeForm createModelAttribute() {
7673
return new EmployeeForm();
7774
}
7875

79-
@InitBinder(value = "employee")
80-
public void initBinder(WebDataBinder binder) {
81-
binder.addValidators(new EmployeeValidator());
82-
}
83-
8476
}

src/main/java/com/devjav/form/EmployeeForm.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@
66
*/
77
package com.devjav.form;
88

9+
import javax.validation.constraints.Pattern;
10+
11+
import org.hibernate.validator.constraints.Email;
12+
import org.hibernate.validator.constraints.NotBlank;
13+
914
/**
1015
*
1116
* @author Pham Thai Thinh
1217
*
1318
*/
1419
public class EmployeeForm {
20+
@NotBlank
1521
private String firstName;
22+
@NotBlank
1623
private String lastName;
24+
@NotBlank
25+
@Pattern(regexp="[0-9]{10}")
1726
private String phone;
1827
private String dob;
28+
@NotBlank
29+
@Email
1930
private String email;
2031
private String joinDate;
2132

src/main/resources/messages.properties

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
#General use
22
error.invalid={0} is invalid format.
33
error.requried={0} is required.
4-
4+
#hibernate validator custom messages
5+
Pattern.employee.phone=Phone is invalid format.
6+
NotBlank.employee.phone=Phone is required
7+
NotBlank.employee.lastName=Last name is required
8+
NotBlank.employee.firstName=First name is required
9+
NotBlank.employee.email=Email name is required
10+
Email.employee.email=Email is invalid format.
511
#Add Employee Form
612
employee.form.title=Add Employee
713
employee.form.firstname=First name

src/main/webapp/WEB-INF/views/add.jsp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
color: red;
99
}
1010
</style>
11+
<c:if test="${not empty message}">
12+
<div id="message" style="background-color: green">${message}</div></c:if>
13+
<spring:url value="/manage/add.do" var="action" />
14+
<form:form action="${action}" commandName="employee">
1115
<spring:hasBindErrors name="employee">
1216
<div class="error">
1317
<h4>Oops!</h4>
1418
<ul>
1519
<c:forEach var="error" items="${errors.allErrors}">
16-
<li><spring:message code="${error.code}"
17-
arguments="${error.arguments}" text="${errMsgObj.defaultMessage}"></spring:message></li>
20+
<li><spring:message code="${error.codes[0]}"
21+
arguments="${error.arguments}" text="${error.defaultMessage}"></spring:message></li>
1822
</c:forEach>
1923
</ul>
2024
</div>
2125
</spring:hasBindErrors>
22-
<c:if test="${not empty message}">
23-
<div id="message" style="background-color: green">${message}</div></c:if>
24-
<spring:url value="/manage/add.do" var="action" />
25-
<form:form action="${action}" commandName="employee">
2626
<div>
2727
<table>
2828
<tr>

0 commit comments

Comments
 (0)