DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG· RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://issues.apache.org/bugzilla/show_bug.cgi?id=34454>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND· INSERTED IN THE BUG DATABASE.
http://issues.apache.org/bugzilla/show_bug.cgi?id=34454 Summary: [contribution] Consecutive Date Validator Product: Struts Version: 1.2.4 Platform: PC OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: Validator Framework AssignedTo: dev@struts.apache.org ReportedBy: [EMAIL PROTECTED] [I sent this originally to struts-user and James Mitchell suggested I put it in as an enhancement so it wouldn't be lost] I had the need today to validate that two dates (start and end) are in the proper relation. Searching the oracle at Google, I was unable to find any solution other than people looking for the same thing and being directed to write their own. So I wrote one today and figured I'd contribute it back so that others can find it and I can also get a little extra scrutiny. This was written for Struts 1.2.4 K.C. Baltz ================== snippet from validator-rules.xml ================ <validator name="consecutiveDates" classname="com.mycompany.web.util.ValidationUtil" method="validateConsecutiveDates" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionMessages, javax.servlet.http.HttpServletRequest" depends="" msg="errors.consecutiveDates"> </validator> =================== Example entry in validation.xml ================ <form name="manageResourcesForm"> <!-- validate that this both a valid date and that it precedes the end date --> <field property="startDate" depends="date,consecutiveDates"> <msg name="consecutiveDates" key="errors.consecutiveDates" /> <arg0 key="admin.editNotice.prompt.startDate" /> <arg1 key="admin.editNotice.prompt.endDate" /> <var> <var-name>secondProperty</var-name> <var-value>endDate</var-value> </var> </field> <!-- need to validate that this is a date --> <field property="endDate" depends="date"> <arg0 key="admin.editNotice.prompt.endDate" /> </field> </form> =============== snippet from ValidationUtil.java ================ public static boolean validateConsecutiveDates(Object bean, ValidatorAction va, Field field, final ActionMessages errors, HttpServletRequest request) { boolean passedValidation = true; String startDateString = ValidatorUtils.getValueAsString(bean, field.getProperty()); String sProperty2 = field.getVarValue("secondProperty"); String endDateString = ValidatorUtils.getValueAsString(bean, sProperty2); if(!GenericValidator.isBlankOrNull(startDateString) && !GenericValidator.isBlankOrNull(endDateString)) { try { Locale locale = RequestUtils.getUserLocale(request, null); DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale); Date startDate = null; Date endDate = null; try { startDate = formatter.parse(startDateString); } catch (ParseException e) { errors.add(field.getArg(0).getKey(), new ActionMessage("errors.date", startDateString)); passedValidation = false; } try { endDate = formatter.parse(endDateString); } catch (ParseException e) { errors.add(field.getArg(1).getKey(), new ActionMessage("errors.date", endDateString)); passedValidation = false; } if( startDate != null && endDate != null && startDate.after(endDate) ) { errors.add(ActionMessages.GLOBAL_MESSAGE, Resources.getActionMessage(request, va, field)); passedValidation = false; } return passedValidation; } catch(Exception e) { log.error("Exception caught trying to perform validateConsecutiveDates", e); errors.add(field.getKey(), Resources.getActionMessage(request, va, field)); return false; } } return true; } ===================================== -- Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are the assignee for the bug, or are watching the assignee. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]