Here is some code:
Code:
Init binder for the date values in the form
@Override
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
//Create a custom binder that will convert a String with pattern dd/MM/yyyy to an appropriate Date object.
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
Code:
Code for validating dates:
private void validatePage1(Object target, Errors errors) {
CampaignWizardData data = (CampaignWizardData) target;
//If the wizard is in update mode, don't do the validation there because the controller will redirect you to that page in case
//of errors, this is due to the fact that a check is done on the existence of a campaign with the filled in name.
if (!data.isUpdate() && !data.isCopy()) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startDate", "errors.startDate","Please fill in a valid start date");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "endDate", "errors.endDate","Please fill in a valid end date");
if (data.getEndDate() != null && data.getStartDate() != null && data.getEndDate().before(data.getStartDate())) {
errors.reject("errors.endDateBeforeStartDate", "The end date should come after the start date");
}else if (data.getEndDate() != null && data.getStartDate() != null && data.getStartDate().before(DateUtils.addDays(DateUtils.truncate(new Date(), Calendar.DATE), 1))) {
errors.reject("errors.startDateBeforeToday", "The start date should be at least one day after today");
}
if (pmgmFacade.checkExistingCampaignName(data.getName())) {
errors.reject("errors.campaignName", "A campaign with this name already exists");
}
}
}
Code:
Displayal of errors in the jsp page:
...
<spring:bind path="campaignData.*">
<c:if test="${not empty status.errorMessages}">
<c:forEach items="${status.errorMessages}" var="error">
<c:out value="${error}" escapeXml="false"/> <br />
</c:forEach>
</c:if>
</spring:bind>
....