KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.springframework.samples.petclinic.validation;
2
3 import org.springframework.samples.petclinic.Pet;
4 import org.springframework.util.StringUtils;
5 import org.springframework.validation.Errors;
6 import org.springframework.validation.Validator;
7
8 /**
9  * <code>Validator</code> for <code>Pet</code> forms.
10  *
11  * @author Ken Krebs
12  * @author Juergen Hoeller
13  */

14 public class PetValidator implements Validator {
15
16     public boolean supports(Class JavaDoc clazz) {
17         return Pet.class.isAssignableFrom(clazz);
18     }
19
20     public void validate(Object JavaDoc obj, Errors errors) {
21         Pet pet = (Pet) obj;
22
23         String JavaDoc name = pet.getName();
24         if (!StringUtils.hasLength(name)) {
25             errors.rejectValue("name", "required", "required");
26         }
27         else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) {
28             errors.rejectValue("name", "duplicate", "already exists");
29         }
30     }
31
32 }
33
Popular Tags