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
src/main/java/fr/plil/sio/web/mvc/NewUserController.java
... | ... | @@ -3,6 +3,8 @@ package fr.plil.sio.web.mvc; |
3 | 3 | import org.springframework.security.access.prepost.PreAuthorize; |
4 | 4 | import org.springframework.stereotype.Controller; |
5 | 5 | import org.springframework.validation.BindingResult; |
6 | +import org.springframework.web.bind.WebDataBinder; | |
7 | +import org.springframework.web.bind.annotation.InitBinder; | |
6 | 8 | import org.springframework.web.bind.annotation.RequestMapping; |
7 | 9 | import org.springframework.web.bind.annotation.RequestMethod; |
8 | 10 | import org.springframework.web.servlet.ModelAndView; |
... | ... | @@ -17,6 +19,11 @@ public class NewUserController { |
17 | 19 | @Resource |
18 | 20 | private UserService userService; |
19 | 21 | |
22 | + @InitBinder | |
23 | + protected void initBinder(WebDataBinder binder) { | |
24 | + binder.addValidators(new PasswordValidator()); | |
25 | + } | |
26 | + | |
20 | 27 | @RequestMapping(method = RequestMethod.GET) |
21 | 28 | public ModelAndView getNewUserForm() { |
22 | 29 | return new ModelAndView("newUser", "userForm", new UserForm()); | ... | ... |
src/main/java/fr/plil/sio/web/mvc/PasswordValidator.java
0 → 100644
... | ... | @@ -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 | 9 | new.user.page.title=New user |
10 | 10 | new.user.main.header=Add a new user |
11 | 11 | new.user.form.submit=Add the user |
12 | -new.user.form.present=user already present | |
13 | 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 | 17 | \ No newline at end of file | ... | ... |