-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserValidator.java
43 lines (31 loc) · 1.17 KB
/
UserValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.CodingDojo.LognadReg.Validator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.CodingDojo.LognadReg.Models.User;
import com.CodingDojo.LognadReg.Service.UserService;
@Component
public class UserValidator implements Validator {
private final UserService userService;
public UserValidator(UserService userService) {
this.userService = userService;
}
// 1
@Override
public boolean supports(Class<?> clazz) {
return User.class.equals(clazz);
}
// 2
@Override
public void validate(Object target, Errors errors) {
User user = (User) target;
if (!user.getPasswordConfirmation().equals(user.getPassword())) {
// 3
errors.rejectValue("passwordConfirmation", "Match");
}
//we can talk to the service and see if any email matches the user from from email
if(this.userService.findByEmail(user.getEmail().toLowerCase()) != null) {
errors.rejectValue("email", "DupeEmail");
}
}
}