Skip to content
Snippets Groups Projects
Commit 80ff54ac authored by Antoine Rey's avatar Antoine Rey
Browse files

Fix #89 Web layer: use @Valid whenever possible

parent a07cf692
Branches
No related tags found
No related merge requests found
......@@ -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 {
......
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment