diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetController.java b/src/main/java/org/springframework/samples/petclinic/web/PetController.java index ea8aeaaa86bf90149bc20f93e803cccc63b78465..bf823023e2f8287a0103a5d37895335dc4b55b15 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetController.java @@ -34,6 +34,8 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.support.SessionStatus; +import javax.validation.Valid; + /** * @author Juergen Hoeller * @author Ken Krebs @@ -57,8 +59,9 @@ public class PetController { } @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { + public void initBinder(WebDataBinder dataBinder) { dataBinder.setDisallowedFields("id"); + dataBinder.setValidator(new PetValidator()); } @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET) @@ -71,8 +74,7 @@ public class PetController { } @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST) - public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { - new PetValidator().validate(pet, result); + public String processCreationForm(@Valid Pet pet, BindingResult result, SessionStatus status) { if (result.hasErrors()) { return "pets/createOrUpdatePetForm"; } else { @@ -90,9 +92,7 @@ public class PetController { } @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST}) - public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { - // we're not using @Valid annotation here because it is easier to define such validation rule in Java - new PetValidator().validate(pet, result); + public String processUpdateForm(@Valid Pet pet, BindingResult result, SessionStatus status) { if (result.hasErrors()) { return "pets/createOrUpdatePetForm"; } else { diff --git a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java index 03a1bca7cf4be0915f77e91ccbf181d9a64e0376..0621c98abb8067cdde276d786b9b76892b2470af 100644 --- a/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java +++ b/src/main/java/org/springframework/samples/petclinic/web/PetValidator.java @@ -18,16 +18,22 @@ package org.springframework.samples.petclinic.web; import org.springframework.samples.petclinic.model.Pet; import org.springframework.util.StringUtils; import org.springframework.validation.Errors; +import org.springframework.validation.Validator; /** * <code>Validator</code> for <code>Pet</code> forms. + * <p> + * We're not using Bean Validation annotations here because it is easier to define such validation rule in Java. + * </p> * * @author Ken Krebs * @author Juergen Hoeller */ -public class PetValidator { +public class PetValidator implements Validator { - public void validate(Pet pet, Errors errors) { + @Override + public void validate(Object obj, Errors errors) { + Pet pet = (Pet) obj; String name = pet.getName(); // name validation if (!StringUtils.hasLength(name)) { @@ -47,4 +53,13 @@ public class PetValidator { } } + /** + * This Validator validates *just* Pet instances + */ + @Override + public boolean supports(Class<?> clazz) { + return Pet.class.equals(clazz); + } + + }