This class level bean validation call is not being called.
Code:
@Constraint(validatedBy = FieldPairRequiredValidator.class)
@Target( {ElementType.TYPE, ElementType.CONSTRUCTOR} )
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldPairRequired
{
public abstract String message() default "When one field is specified, the second must be specified as well.";
public abstract Class<?>[] groups() default {};
public abstract Class<? extends Payload>[] payload() default {};
public String firstField();
public String secondField();
}
Code:
public class FieldPairRequiredValidator implements ConstraintValidator<FieldPairRequired, Object>
{
private String firstFieldName;
private String secondFieldName;
public void initialize(FieldPairRequired constraintAnnotation)
{
firstFieldName = constraintAnnotation.firstField();
secondFieldName = constraintAnnotation.secondField();
}
public boolean isValid(Object value, ConstraintValidatorContext context)
{
try
{
Object firstField = BeanUtils.getProperty(value, firstFieldName);
Object secondField = BeanUtils.getProperty(value, secondFieldName);
if((null == firstField && null != secondField) || (null == secondField && null != firstField))
{
return false;
}
}
catch(Exception e)
{
System.err.println("Error: " + e);
//ignore
}
return true;
}
}
It is being called like this:
Code:
@FieldPairRequired(firstField = "id", secondField="secondId", message="Both Id and second Id must be specified when one is specified.")
@ManagedBean
@RequestScoped
public class PersonSearch
The only way I can get it to be called is when I place it on a field. I want it to validate a class not an individual field. Any ideas what is going on?