Hi, I'm new in the world hibernate validator I'm implementing a method of type org.springframework.validation.Validator I should take the Locale of the JVM but no file properties.
Even utilizando Local local = LocaleContextHolder.getLocale (); ResourceBundle method returns the default locale of the JVM and then subsequently also MessageInterpolator wrong
the code I use is:
public class InsertUtenteValidator implements org.springframework.validation.Validator, InitializingBean { private static final Log logger = LogFactory.getLog(InsertUtenteValidator.class.getName()); private Validator validator;
public void afterPropertiesSet() throws Exception {
} public boolean supports(Class clazz) { return UtenteCommand.class.isAssignableFrom(clazz); } public void validate(Object object, Errors errors) { Locale locale = LocaleContextHolder.getLocale(); logger.info("Llocale: '" + locale ); ResourceBundle bundle = ResourceBundle.getBundle("org/hibernate/validator/ValidationMessages", locale); MessageInterpolator interpolator = new ResourceBundleMessageInterpolator(bundle); ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); validator = validatorFactory.usingContext().messageInterpolator(interpolator).getValidator(); UtenteCommand command = (UtenteCommand) object; Utente ute = command.getUtente(); Set<ConstraintViolation<Utente>> violations = validator.validate(ute); for (ConstraintViolation<Utente> constraintViolation : violations) { String propertyPath = constraintViolation.getPropertyPath().toString(); logger.info("propertyPath: '" + propertyPath ); String message = constraintViolation.getMessage(); logger.info("message: '" + message ); errors.rejectValue("utente."+propertyPath,null, message); } } }
|