1 package org.springframework.samples.petclinic.validation;
2
3 import org.springframework.samples.petclinic.Owner;
4 import org.springframework.util.StringUtils;
5 import org.springframework.validation.Errors;
6
7
8
9
10
11
12
13 public class OwnerValidator {
14
15 public void validate(Owner owner, Errors errors) {
16 if (!StringUtils.hasLength(owner.getFirstName())) {
17 errors.rejectValue("firstName", "required", "required");
18 }
19 if (!StringUtils.hasLength(owner.getLastName())) {
20 errors.rejectValue("lastName", "required", "required");
21 }
22 if (!StringUtils.hasLength(owner.getAddress())) {
23 errors.rejectValue("address", "required", "required");
24 }
25 if (!StringUtils.hasLength(owner.getCity())) {
26 errors.rejectValue("city", "required", "required");
27 }
28
29 String telephone = owner.getTelephone();
30 if (!StringUtils.hasLength(telephone)) {
31 errors.rejectValue("telephone", "required", "required");
32 }
33 else {
34 for (int i = 0; i < telephone.length(); ++i) {
35 if ((Character.isDigit(telephone.charAt(i))) == false) {
36 errors.rejectValue("telephone", "nonNumeric", "non-numeric");
37 break;
38 }
39 }
40 }
41 }
42
43 }