-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f455b0
commit 9cd2c49
Showing
7 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* devjav [http://devjav.com] | ||
* Copyright (C) 2014-2014 Pham Thai Thinh | ||
* Contact:[email protected] | ||
* | ||
*/ | ||
package com.devjav.validator; | ||
|
||
import org.springframework.validation.Errors; | ||
import org.springframework.validation.ValidationUtils; | ||
import org.springframework.validation.Validator; | ||
|
||
import com.devjav.form.EmployeeForm; | ||
|
||
/** | ||
* | ||
* @author Pham Thai Thinh | ||
* | ||
*/ | ||
public class EmployeeValidator implements Validator { | ||
public boolean supports(Class<?> clazz) { | ||
return clazz.isAssignableFrom(EmployeeForm.class); | ||
} | ||
|
||
public void validate(Object form, Errors errors) { | ||
EmployeeForm employeeForm = (EmployeeForm) form; | ||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", | ||
"error.requried", new Object[] { "First name" }); | ||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", | ||
"error.requried", new Object[] { "Last name" }); | ||
|
||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "phone", | ||
"error.requried", new Object[] { "Phone" }); | ||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", | ||
"error.requried", new Object[] { "Email" }); | ||
|
||
if (!errors.hasFieldErrors("email")) { | ||
if (!PatternValidator.validateEmail(employeeForm.getEmail())) { | ||
errors.rejectValue("email", "error.invalid", | ||
new Object[] { "Email" }, "Email is invalid format"); | ||
} | ||
} | ||
if (!errors.hasFieldErrors("phone")) { | ||
if (!PatternValidator.validatePhone(employeeForm.getPhone())) { | ||
errors.rejectValue("phone", "error.invalid", | ||
new Object[] { "Phone" }, "Phone is invalid format"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* devjav [http://devjav.com] | ||
* Copyright (C) 2014-2014 Pham Thai Thinh | ||
* Contact:[email protected] | ||
* | ||
*/ | ||
package com.devjav.validator; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* | ||
* @author Pham Thai Thinh | ||
* | ||
*/ | ||
public class PatternValidator { | ||
private static String PHONE = "[0-9]{10}"; | ||
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" | ||
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | ||
|
||
public static boolean validateEmail(String email) { | ||
Pattern pattern = Pattern.compile(EMAIL_PATTERN); | ||
Matcher matcher = pattern.matcher(email); | ||
return matcher.matches(); | ||
} | ||
public static boolean validatePhone(String phone) { | ||
Pattern pattern = Pattern.compile(PHONE); | ||
Matcher matcher = pattern.matcher(phone); | ||
return matcher.matches(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#General use | ||
error.invalid={0} is invalid format. | ||
error.requried={0} is required. | ||
|
||
#Add Employee Form | ||
employee.form.title=Add Employee | ||
employee.form.firstname=First name | ||
employee.form.lastname=Last Name | ||
employee.form.phone=Phone | ||
employee.form.email=Email | ||
#End Add Employee Form |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters