Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.makeandbuild.validation.validator;
 
 
JSR-303 spec bean validator. validates a bean based on the JSR-303 annotations that are present in the bean. User: Jeremy Dyer Date: 3/7/14 Time: 11:19 AM
public class JSR303BeanValidator
    private Validator validator;
    public void afterPropertiesSet() throws Exception {
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
         = validatorFactory.usingContext().getValidator();
    }
    @SuppressWarnings("rawtypes")
    public boolean supports(Class clazz) {
        return true;
    }
    public void validate(Object targetErrors errors) {
        Set<ConstraintViolation<Object>> constraintViolations = .validate(target);
        for (ConstraintViolation<ObjectconstraintViolation : constraintViolations) {
            //TODO: Need clever mechanism for injecting error messages either via the Annotation (already supported)
            // or from a properties file, URL, DB, Ask Jeeves, or whatever ....
            String propertyPath = constraintViolation.getPropertyPath().toString();
            String message = constraintViolation.getMessage();
            errors.rejectValue(propertyPath""message);
        }
    }
New to GrepCode? Check out our FAQ X