I have a custom validator like this:
@Retention(RetentionPolicy.RUNTIME)
@NotBlank(message = "{state.NotBlank}") // Err...it doesn't work
@Constraint(validatedBy = { State.Validator.class })
public @interface State {
String message() default "{state.invalid}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
@Component
final class Validator implements ConstraintValidator<State, String> {
@Override
public void initialize(final State constraintAnnotation) { }
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
// return true or false based on some rules
}
}
}
But there is not way I can make it work annotating State
with @NotBlank
— since the later doesn't have the ElementType.TYPE
in @Target
), but annotating the fields themselves with @NotBlank
.
The reason I would like to have it in State
is because I have to repeat the same constraint to several fields, so if State
could be annotated with @NotBlank
would be perfect.
Any clues?