1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.promotego.validators;
20
21 import org.promotego.beans.UserHolder;
22 import org.promotego.formbackingbeans.RadiusSearchBean;
23 import org.springframework.beans.factory.annotation.Required;
24 import org.springframework.validation.Errors;
25 import org.springframework.validation.ValidationUtils;
26 import org.springframework.validation.Validator;
27
28 public class RadiusSearchValidator implements Validator
29 {
30 private UserHolder m_userHolder;
31
32 public boolean supports(@SuppressWarnings("unchecked") Class clazz)
33 {
34 return RadiusSearchBean.class.isAssignableFrom(clazz);
35 }
36
37 public void validate(Object target, Errors errors)
38 {
39 ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "required", "Field is required.");
40 ValidationUtils.rejectIfEmptyOrWhitespace(errors, "miles", "required", "Field is required.");
41
42 assert m_userHolder.getUser() != null : "User object may not be null";
43
44 RadiusSearchBean radiusSearch = (RadiusSearchBean)target;
45 if(!m_userHolder.getUser().equals(radiusSearch.getAddress().getUser()))
46 {
47
48
49
50 errors.rejectValue("address", "invalidAddress", "Address is invalid");
51 }
52 }
53
54 @Required
55 public void setUserHolder(UserHolder userHolder)
56 {
57 m_userHolder = userHolder;
58 }
59 }