16
16
public class UserValidator implements Validator {
17
17
18
18
public static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern .compile ("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\ .[A-Z]{2,6}$" , Pattern .CASE_INSENSITIVE );
19
+ private static final String NOT_EMPTY_STRING = "NotEmpty" ;
20
+ private static final String EMAIL_STRING = "email" ;
21
+ private static final String USERNAME_STRING = "username" ;
19
22
20
23
@ Autowired
21
24
private UserService userService ;
@@ -28,28 +31,29 @@ public boolean supports(Class<?> aClass) {
28
31
@ Override
29
32
public void validate (Object o , Errors errors ) {
30
33
User user = (User ) o ;
34
+
31
35
32
36
// Email
33
- ValidationUtils .rejectIfEmptyOrWhitespace (errors , "email" , "NotEmpty" );
37
+ ValidationUtils .rejectIfEmptyOrWhitespace (errors , EMAIL_STRING , NOT_EMPTY_STRING );
34
38
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX .matcher (user .getEmail ());
35
39
if (! matcher .find ()) {
36
- errors .rejectValue ("email" , "Not a valid email address." );
40
+ errors .rejectValue (EMAIL_STRING , "Not a valid email address." );
37
41
}
38
42
if (userService .findByEmail (user .getEmail ()) != null ) {
39
- errors .rejectValue ("email" , "Duplicate.userForm.email" );
43
+ errors .rejectValue (EMAIL_STRING , "Duplicate.userForm.email" );
40
44
}
41
45
42
46
// Username
43
- ValidationUtils .rejectIfEmptyOrWhitespace (errors , "username" , "NotEmpty" );
47
+ ValidationUtils .rejectIfEmptyOrWhitespace (errors , USERNAME_STRING , NOT_EMPTY_STRING );
44
48
if (user .getUsername ().length () < 6 || user .getUsername ().length () > 32 ) {
45
- errors .rejectValue ("username" , "Size.userForm.username" );
49
+ errors .rejectValue (USERNAME_STRING , "Size.userForm.username" );
46
50
}
47
51
if (userService .findByUsername (user .getUsername ()) != null ) {
48
- errors .rejectValue ("username" , "Duplicate.userForm.username" );
52
+ errors .rejectValue (USERNAME_STRING , "Duplicate.userForm.username" );
49
53
}
50
54
51
55
// Password
52
- ValidationUtils .rejectIfEmptyOrWhitespace (errors , "password" , "NotEmpty" );
56
+ ValidationUtils .rejectIfEmptyOrWhitespace (errors , "password" , NOT_EMPTY_STRING );
53
57
if (user .getPassword ().length () < 8 || user .getPassword ().length () > 32 ) {
54
58
errors .rejectValue ("password" , "Size.userForm.password" );
55
59
}
0 commit comments