I have a need to have the same annotation used in multiple fields / properties, but the validation message to be slightly different which depends on another property, or potentially the lable associated with the input field.
To bettter illustrate, suppose I have a @NotFuture annotation ( Why the standard JSR-303 annotation does not even provide this, which seems to be required in almost all projects that I use, is a different story ), as below:
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.ReportAsSingleViolation;
import javax.validation.constraints.Future;
import org.hibernate.validator.constraints.CompositionType;
import org.hibernate.validator.constraints.ConstraintComposition;
/**
* The annotated element must be a date that is not in the future and must not be null.
* Because this is simply a composition from @Future, this therefore supports
* the same data-types as @Future
*/
@ConstraintComposition(CompositionType.ALL_FALSE)
@Future
@ReportAsSingleViolation
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {})
public @interface NotFutureNotNull {
public abstract String message() default "{constraints.notFutureNotNull.message}";
public abstract Class<?>[] groups() default { };
public abstract Class<? extends Payload>[] payload() default { };
}
Then one of the properties in my backing bean is annotated as such:
@NotFuture(message="Notification Date cannot be a future date")
private Date notificationDate;
All good and fine, JSF will show the message as shown above when it goes through the validation lifecycle and rendered back to the page.
Now I sort of wanted to achieve something like the one below, if it is possible at all:
@NotFuture(message="#{incidentLabel} cannot be a future date")
private Date incidentDate;
The intention is that the message is determined at runtime, as the {incidentLabel} is really derived from another property ( e.g. an incidentType ). Is this even possible ? If not, does this mean we cannot really use JSR-303 annotation when we need a run-time derived validation message ?