Commit 5ab9e3a35d51773e14c5ffd70288fe8dc85650d5
1 parent
19426ff3
Add support for dual validations
Showing
4 changed files
with
49 additions
and
1 deletions
Show diff stats
pom.xml
@@ -75,6 +75,10 @@ | @@ -75,6 +75,10 @@ | ||
75 | <artifactId>bootstrap</artifactId> | 75 | <artifactId>bootstrap</artifactId> |
76 | <version>3.3.7</version> | 76 | <version>3.3.7</version> |
77 | </dependency> | 77 | </dependency> |
78 | + <dependency> | ||
79 | + <groupId>org.hibernate</groupId> | ||
80 | + <artifactId>hibernate-validator</artifactId> | ||
81 | + </dependency> | ||
78 | </dependencies> | 82 | </dependencies> |
79 | 83 | ||
80 | <build> | 84 | <build> |
src/main/java/fr/plil/sio/web/mvc/NewUserController.java
@@ -3,6 +3,8 @@ package fr.plil.sio.web.mvc; | @@ -3,6 +3,8 @@ package fr.plil.sio.web.mvc; | ||
3 | import org.springframework.security.access.prepost.PreAuthorize; | 3 | import org.springframework.security.access.prepost.PreAuthorize; |
4 | import org.springframework.stereotype.Controller; | 4 | import org.springframework.stereotype.Controller; |
5 | import org.springframework.validation.BindingResult; | 5 | import org.springframework.validation.BindingResult; |
6 | +import org.springframework.web.bind.WebDataBinder; | ||
7 | +import org.springframework.web.bind.annotation.InitBinder; | ||
6 | import org.springframework.web.bind.annotation.RequestMapping; | 8 | import org.springframework.web.bind.annotation.RequestMapping; |
7 | import org.springframework.web.bind.annotation.RequestMethod; | 9 | import org.springframework.web.bind.annotation.RequestMethod; |
8 | import org.springframework.web.servlet.ModelAndView; | 10 | import org.springframework.web.servlet.ModelAndView; |
@@ -17,6 +19,11 @@ public class NewUserController { | @@ -17,6 +19,11 @@ public class NewUserController { | ||
17 | @Resource | 19 | @Resource |
18 | private UserService userService; | 20 | private UserService userService; |
19 | 21 | ||
22 | + @InitBinder | ||
23 | + protected void initBinder(WebDataBinder binder) { | ||
24 | + binder.addValidators(new PasswordValidator()); | ||
25 | + } | ||
26 | + | ||
20 | @RequestMapping(method = RequestMethod.GET) | 27 | @RequestMapping(method = RequestMethod.GET) |
21 | public ModelAndView getNewUserForm() { | 28 | public ModelAndView getNewUserForm() { |
22 | return new ModelAndView("newUser", "userForm", new UserForm()); | 29 | return new ModelAndView("newUser", "userForm", new UserForm()); |
src/main/java/fr/plil/sio/web/mvc/PasswordValidator.java
0 → 100644
@@ -0,0 +1,34 @@ | @@ -0,0 +1,34 @@ | ||
1 | +package fr.plil.sio.web.mvc; | ||
2 | + | ||
3 | +import org.springframework.validation.Errors; | ||
4 | +import org.springframework.validation.Validator; | ||
5 | + | ||
6 | +public class PasswordValidator implements Validator { | ||
7 | + | ||
8 | + | ||
9 | + @Override | ||
10 | + public boolean supports(Class<?> aClass) { | ||
11 | + return UserForm.class.equals(aClass); | ||
12 | + } | ||
13 | + | ||
14 | + @Override | ||
15 | + public void validate(Object o, Errors errors) { | ||
16 | + UserForm userForm = (UserForm)o; | ||
17 | + String password = userForm.getPassword(); | ||
18 | + | ||
19 | + int upperCase = 0; | ||
20 | + int lowerCase = 0; | ||
21 | + | ||
22 | + for (int k = 0; k < password.length(); k++) { | ||
23 | + if (Character.isUpperCase(password.charAt(k))) upperCase++; | ||
24 | + if (Character.isLowerCase(password.charAt(k))) lowerCase++; | ||
25 | + } | ||
26 | + | ||
27 | + if(upperCase == 0) { | ||
28 | + errors.rejectValue("password", "password.validator.uppercase"); | ||
29 | + } | ||
30 | + if(lowerCase == 0) { | ||
31 | + errors.rejectValue("password", "password.validator.lowercase"); | ||
32 | + } | ||
33 | + } | ||
34 | +} |
src/main/resources/messages.properties
@@ -9,4 +9,7 @@ view.users.main.logout=Logout | @@ -9,4 +9,7 @@ view.users.main.logout=Logout | ||
9 | new.user.page.title=New user | 9 | new.user.page.title=New user |
10 | new.user.main.header=Add a new user | 10 | new.user.main.header=Add a new user |
11 | new.user.form.submit=Add the user | 11 | new.user.form.submit=Add the user |
12 | -new.user.form.present=user already present | ||
13 | \ No newline at end of file | 12 | \ No newline at end of file |
13 | +new.user.form.present=user already present | ||
14 | + | ||
15 | +password.validator.uppercase=At least one uppercase in password | ||
16 | +password.validator.lowercase=At least one lowercase in password | ||
14 | \ No newline at end of file | 17 | \ No newline at end of file |