File tree Expand file tree Collapse file tree 4 files changed +24
-15
lines changed
Expand file tree Collapse file tree 4 files changed +24
-15
lines changed Original file line number Diff line number Diff line change 1313import org .springframework .stereotype .Controller ;
1414import org .springframework .ui .Model ;
1515import org .springframework .validation .BindingResult ;
16- import org .springframework .web .bind .WebDataBinder ;
17- import org .springframework .web .bind .annotation .InitBinder ;
1816import org .springframework .web .bind .annotation .ModelAttribute ;
1917import org .springframework .web .bind .annotation .RequestMapping ;
2018import org .springframework .web .bind .annotation .RequestMethod ;
2119
2220import com .devjav .domain .Employee ;
2321import com .devjav .form .EmployeeForm ;
2422import 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}
Original file line number Diff line number Diff line change 66 */
77package 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 */
1419public 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
Original file line number Diff line number Diff line change 11# General use
22error.invalid ={0} is invalid format.
33error.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
612employee.form.title =Add Employee
713employee.form.firstname =First name
Original file line number Diff line number Diff line change 88color : 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 >
You can’t perform that action at this time.
0 commit comments