01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.sample.petclinic.validation;
18:
19: import org.compass.sample.petclinic.Owner;
20: import org.springframework.util.StringUtils;
21: import org.springframework.validation.Errors;
22: import org.springframework.validation.ValidationUtils;
23: import org.springframework.validation.Validator;
24:
25: /**
26: * <code>Validator</code> for <code>Owner</code> forms.
27: *
28: * @author Ken Krebs
29: * @author Juergen Hoeller
30: */
31:
32: public class OwnerValidator implements Validator {
33:
34: public boolean supports(Class clazz) {
35: return Owner.class.isAssignableFrom(clazz);
36: }
37:
38: public void validate(Object obj, Errors errors) {
39: Owner owner = (Owner) obj;
40: ValidationUtils.rejectIfEmpty(errors, "firstName", "required",
41: "required");
42: ValidationUtils.rejectIfEmpty(errors, "lastName", "required",
43: "required");
44: ValidationUtils.rejectIfEmpty(errors, "address", "required",
45: "required");
46: ValidationUtils.rejectIfEmpty(errors, "city", "required",
47: "required");
48: String telephone = owner.getTelephone();
49: if (!StringUtils.hasLength(telephone)) {
50: errors.rejectValue("telephone", "required", "required");
51: } else {
52: for (int i = 0; i < telephone.length(); ++i) {
53: if ((Character.isDigit(telephone.charAt(i))) == false) {
54: errors.rejectValue("telephone", "nonNumeric",
55: "non-numeric");
56: break;
57: }
58: }
59: }
60: }
61: }
|