Fix #89 Web layer: use @Valid whenever possible

This commit is contained in:
Antoine Rey 2015-06-29 08:37:29 +02:00
parent a07cf69292
commit 80ff54ac03
2 changed files with 23 additions and 8 deletions

View file

@ -34,6 +34,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.bind.support.SessionStatus;
import javax.validation.Valid;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Ken Krebs * @author Ken Krebs
@ -57,8 +59,9 @@ public class PetController {
} }
@InitBinder @InitBinder
public void setAllowedFields(WebDataBinder dataBinder) { public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id"); dataBinder.setDisallowedFields("id");
dataBinder.setValidator(new PetValidator());
} }
@RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.GET) @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) @RequestMapping(value = "/owners/{ownerId}/pets/new", method = RequestMethod.POST)
public String processCreationForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { public String processCreationForm(@Valid Pet pet, BindingResult result, SessionStatus status) {
new PetValidator().validate(pet, result);
if (result.hasErrors()) { if (result.hasErrors()) {
return "pets/createOrUpdatePetForm"; return "pets/createOrUpdatePetForm";
} else { } else {
@ -90,9 +92,7 @@ public class PetController {
} }
@RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST}) @RequestMapping(value = "/owners/{ownerId}/pets/{petId}/edit", method = {RequestMethod.PUT, RequestMethod.POST})
public String processUpdateForm(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) { public String processUpdateForm(@Valid 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);
if (result.hasErrors()) { if (result.hasErrors()) {
return "pets/createOrUpdatePetForm"; return "pets/createOrUpdatePetForm";
} else { } else {

View file

@ -18,16 +18,22 @@ package org.springframework.samples.petclinic.web;
import org.springframework.samples.petclinic.model.Pet; import org.springframework.samples.petclinic.model.Pet;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
/** /**
* <code>Validator</code> for <code>Pet</code> forms. * <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 Ken Krebs
* @author Juergen Hoeller * @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(); String name = pet.getName();
// name validation // name validation
if (!StringUtils.hasLength(name)) { 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);
}
} }