View Javadoc

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   
7   /**
8    * <code>Validator</code> for <code>Pet</code> forms.
9    *
10   * @author Ken Krebs
11   * @author Juergen Hoeller
12   */
13  public class PetValidator {
14  
15  	public void validate(Pet pet, Errors errors) {
16  		String name = pet.getName();
17  		if (!StringUtils.hasLength(name)) {
18  			errors.rejectValue("name", "required", "required");
19  		}
20  		else if (pet.isNew() && pet.getOwner().getPet(name, true) != null) {
21  			errors.rejectValue("name", "duplicate", "already exists");
22  		}
23  	}
24  
25  }