1
2
3
4 package org.mod4j.runtime.validation;
5
6 import org.springframework.util.Assert;
7 import org.springframework.util.StringUtils;
8 import org.springframework.validation.Errors;
9 import org.springframework.validation.Validator;
10
11
12
13
14
15 public class NotNullValidator implements Validator {
16 private String field;
17
18 private Class clazz;
19
20
21
22
23
24 public NotNullValidator(Class clazz, String field) {
25 this.clazz = clazz;
26 this.field = field;
27 }
28
29
30
31
32 public boolean supports(Class clazz) {
33 return this.clazz.isAssignableFrom(clazz);
34 }
35
36
37
38
39 public void validate(Object target, Errors errors) {
40 Assert.notNull(errors, "Errors object must not be null");
41 Object value = errors.getFieldValue(field);
42 if (value == null || !StringUtils.hasLength(value.toString())) {
43 errors.rejectValue(field, "field.required", field + " should not be null");
44 }
45 }
46 }