I'm looking to do a little custom validation with JSR-303 javax.validation
.
I have a field. And If a certain value is entered into this field I want to require that a few other fields are not null
.
I'm trying to figure this out. Not sure exactly what I would call this to help find an explanation.
Any help would be appreciated. I am pretty new to this.
At the moment I'm thinking a Custom Constraint. But I'm not sure how to test the value of the dependent field from within the annotation. Basically I'm not sure how to access the panel object from the annotation.
public class StatusValidator implements ConstraintValidator<NotNull, String> {
@Override
public void initialize(NotNull constraintAnnotation) {}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if ("Canceled".equals(panel.status.getValue())) {
if (value != null) {
return true;
}
} else {
return false;
}
}
}
It's the panel.status.getValue();
giving me trouble.. not sure how to accomplish this.
Object
). In this case, you even don't need to use reflection for getting the values but in this case validator become less generic 2) useBeanWrapperImp
from Spring Framework (or other libraries) and itsgetPropertyValue()
method. In this case you will be able to get a value asObject
and cast to any type that you need. – Ist