Hello,
I have 2 models in my architecture, model 1 is the bean that represents the data in my html form. Model 2 represents that data that is stored in the database. In this case I'll call UserBean the model from the html form and User represents the model object to be stored in the database. UserBean uses User to retrieve user data such as name and email. Keeping in mind DRY (Don't repeat yourself) I'd like to annotate my User object and have the UserBean object reference the User object's annotated types -- see the @ValidateReference constraint annotation below.
I have a UserBean and User class that looks like this
Code:
public class UserBean {
private User user = new User();
@ValidateReference(refClass=User.class, property = "name")
public String getName() {
return user.getName();
}
public void setName(String name) {
this.user.setName( name);
}
@ValidateReference(refClass=User.class, property = "email")
public String getEmail() {
return user.getEmail();
}
public void setEmail(String email) {
this.user.setEmail(email);
}
public User getUser() { return user; }
}
//======================================================
public class User
{
private String name;
private String email;
@Size(min = 3, message = "Name should at least be 3 characters long")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Pattern(regexp = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", message = "This is not a valid email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
I created the following constraint annotation ValidateReference and ValidateReferenceValidator. My question is, how do I get access to the Locale inside the ValidateReferenceValidator class as well as fill in all the error message information so the error in the actual annotation constraint bubbles up to the final output error message? I sense I'm going down the wrong path in this solution. Please suggest a better path if necessary.
Code:
@Target({TYPE, METHOD, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = ValidateReferenceValidator.class)
@Documented
public @interface ValidateReference
{
String message() default "message from ValidateReference";
Class<?> refClass() ;
String property() ;
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
=======================================================
public class ValidateReferenceValidator implements ConstraintValidator<ValidateReference, Object>
{
Class<?> theClass;
String property;
Class<?>[] groups;
public void initialize(ValidateReference validateReference)
{
theClass = validateReference.refClass();
property = validateReference.property();
groups = validateReference.groups();
// I need help in here to get the Locale information or possibly in the method below.
}
public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext)
{
HibernateConstraintValidatorContext hibernateContext =
constraintValidatorContext.unwrap( HibernateConstraintValidatorContext.class );
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<? extends ConstraintViolation<?>> results = validator.validateValue(theClass, property, o, groups);
if (!results.isEmpty())
{
// I need help in here to fill in all the error data from the actual constraint annotations that failed.
return false;
}
return true;
}
}