How to trigger custom annotated date validator.
samwun9988 Mar 1, 2011 2:52 AMHi,
With jsf2.0, the xhtml webpage I have written contains the following items:
1. date field
2. a submit button
I have written a custom date annotation validation (based on special business rules) class.
When the submit button is clicked, I would expect it will trigger the custom date validator first, if date is valid, it will popup a simple dialog.
The problem is when I click the submit button, there is no trace go into the validator class before popup the dialog.
here is the portion of xhtml code:
<p:dateSelector id="consentDate" label="Patient withdrawal consent date" mandatory="true" value="#{patientSearchController.bean.consentDate}"/>
<div class="submitButtons">
<h:panelGroup rendered="#{patientSearchController.showWithdrawal}">
<h:commandButton actionListener="#{patientSearchController.consentDateWithdrawal}" value="Submit Withdrawal">
<f:attribute name="patient" value="#{patient}" />
</h:commandButton>
</h:panelGroup>
</div>
<p:popup controller="#{patientSearchController}" />
Annotated vadliation:
@PatientSearchBeanValid()
public class PatientSearchPageBean implements IBaseBean{
private static final String CONSENT_DATE_FIELD = "Withdrawal consent date";
@WithdrawalConsentDateString(field=CONSENT_DATE_FIELD)
private String consentDate;
WithdrawalConsentDateString class:
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.TYPE, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.METHOD,
ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE, ElementType.PARAMETER })
@Constraint(validatedBy = WithdrawalConsentDateStringValidator.class)
@DateString
public @interface WithdrawalConsentDateString {
public static final String CONSENT_DATE_IN_FUTURE = "ConsentDateInFuture";
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
String field() default "";
}
WithdrawalConsentDateStringValidator class:
public class WithdrawalConsentDateStringValidator implements ConstraintValidator<WithdrawalConsentDateString, String> {
private String fieldName;
@Override
public void initialize(WithdrawalConsentDateString arg0) {
fieldName = arg0.field();
System.out.println(" ========== WithdrawalConsentDateStringValidator 000==========");
}
@Override
public boolean isValid(String dateStr, ConstraintValidatorContext context) {
boolean valid = true;
System.out.println(" ========== WithdrawalConsentDateStringValidator ==========");
List<String> errorMsgs = DateStringValidator.validateDateString(dateStr, fieldName);
if(CollectionUtils.isNotEmpty(errorMsgs)){
valid = false;
for (String errMsg : errorMsgs) {
ValidatorUtils.addMessage(context,errMsg);
}
}
Date consentDate = DateUtils.convertStringToDate(dateStr);
if (!StringUtils.isEmpty(dateStr)) {
if (consentDate != null) {
// Must in the past
Date today = DateUtils.getCurrentDate();
if (DateUtils.afterDay(consentDate, today)) {
ValidatorUtils.addMessage(context, WithdrawalConsentDateString.CONSENT_DATE_IN_FUTURE);
valid = false;
}
}
}
return valid;
}
}
When the submit button is clickec, I don't see the Sytem.out prints out from the console.
I suspect the vaildator is never get called.
Is there anything wrong with my validation code setup?
Thanks a lot
Sam