Created
October 18, 2011 12:39
-
-
Save xtrycatchx/1295321 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package paul.sydney.controller.validation; | |
import java.util.Set; | |
import javax.validation.ConstraintViolation; | |
import javax.validation.Validation; | |
import javax.validation.ValidatorFactory; | |
import javax.validation.Validator; | |
import org.springframework.beans.factory.InitializingBean; | |
import org.springframework.stereotype.Component; | |
import org.springframework.validation.Errors; | |
/** | |
* GenericValidator class, validates objects during submission | |
* Copyright : adobocode.com , 2010 | |
* | |
* @author Paul Sydney Orozco | xtrycatchx@gmail.com | |
* | |
*/ | |
@Component("genericValidator") | |
public class GenericValidator implements org.springframework.validation.Validator, InitializingBean, IGenericValidator { | |
private Validator validator; | |
public void afterPropertiesSet() throws Exception { | |
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); | |
validator = validatorFactory.usingContext().getValidator(); | |
} | |
@SuppressWarnings("unchecked") | |
public boolean supports(Class clazz) { | |
return true; | |
} | |
public void validate(Object target, Errors errors) { | |
Set<ConstraintViolatio<Object>> constraintViolations = validator.validate(target); | |
for (ConstraintViolation<Object> constraintViolation : constraintViolations) { | |
String propertyPath = constraintViolation.getPropertyPath().toString(); | |
String message = constraintViolation.getMessage(); | |
errors.rejectValue(propertyPath, "", message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment