dimanche 28 juin 2015

Java Custom Contraint - Validate only if other annotation is passed

Situation: I have a basic custom validation setup as below:

@NotNullOrEmpty(message="{err.msg.required}", fieldName="Email")
@ValidEmail(message="{err.msg.validEmail}", fieldName="Email")
private String email;

However, if the email is empty, BOTH are triggered because of course, an empty email is not a valid email.

Question: How do I trigger only @NotNullOrEmpty if its empty and only trigger @ValidEmail if it's non-empty?

Or is it possible to like

@Annotation1
@Annotation2 //trigger only if value has passed @Annotation1

What I have in mind is to edit my ValidEmailValidator.isValid() to return true if email is empty, but what if I want to validate empty emails in the future?

Aucun commentaire:

Enregistrer un commentaire