-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemValidator.java
50 lines (22 loc) · 998 Bytes
/
ItemValidator.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
44
45
46
47
48
49
package eMarket.controller;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class ItemValidator implements Validator {
public boolean supports(Class<?> clazz) {
return ItemFormDto.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ItemFormDto dto = (ItemFormDto) target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "productId", "", "Field cannot be empty.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "amount", "", "Field cannot be empty.");
if (dto.getAmount() <= 0) {
errors.rejectValue("amount", "", "Please Enter a Positive Amount");
}
// no item selected
if(dto.getProductId() < 0) {
errors.rejectValue("productId","","Please Select A product");
}
}
}