KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > validation > OwnerValidator


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 import org.springframework.validation.ValidationUtils;
7 import org.springframework.validation.Validator;
8
9 /**
10  * <code>Validator</code> for <code>Owner</code> forms.
11  *
12  * @author Ken Krebs
13  * @author Juergen Hoeller
14  */

15 public class OwnerValidator implements Validator {
16
17     public boolean supports(Class JavaDoc clazz) {
18         return Owner.class.isAssignableFrom(clazz);
19     }
20
21     public void validate(Object JavaDoc obj, Errors errors) {
22         Owner owner = (Owner) obj;
23
24         ValidationUtils.rejectIfEmpty(errors, "firstName", "required", "required");
25         ValidationUtils.rejectIfEmpty(errors, "lastName", "required", "required");
26         ValidationUtils.rejectIfEmpty(errors, "address", "required", "required");
27         ValidationUtils.rejectIfEmpty(errors, "city", "required", "required");
28
29         String JavaDoc 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 }
44
Popular Tags