-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Our web application uses internationalization and dates in most countries are in dd/MM/yyyy format where in the U.S. the format is MM/dd/yyyy. I have specified the date format using pattern string stored in a i3-labels.properties file, i.e. ui.dateformat=MM/dd/yyyy in en_US and ui.dateformat=dd/MM/yyyy in en_CA. Using the datebox without any contraints and entering a date of 12/13/2010. For Canada, ZK displays an error message, "You must specify a date, Format=dd/MM/yyy." For the U.S. the date is considered valid. However, I want to be able to customize the display of the validation error. Instead of a balloon type display, I prefer to display the error message in a Label.
To do this, the documentation says to create my own Constraint that implements CustomConstraint. Overriding, showCustomError(). I have been able to get most validation errors like NO FUTURE, NO EMPTY, NO TODAY to display in my Label component. However, after linking my custom constraint class to the datebox, the format validation no longer appears nor the the error message display in my Label field. How can I get the error message to display in my label field for illegal date values?
I have debugged my constraint and found the WrongValueException to be null, thus no error text to display. Please note my constraint extends SimpleDateContraint class and uses its validation method. Here is my code:
ZUL
<zk> <window id="win" apply="com.app.MyController" > <datebox id="dob" width="80px" visible="true" format="${labels.ui.locale.dateformat}" lenient="false"/> <button id="submitbtn" label="Submit" /> </window> </zk>
package com.app.MyController; //skipped imports to keep code short public class MyController extends GenericTestController { private static final long serialVersionUID = 1L; private static Logger log = Logger.getLogger(MyController.class); // Form fields; auto-wired. private Datebox dob; private Label doberr; private Button submitbtn; private Window win; /** * Default constructor. Does nothing. */ public MyController() { } /** * Called by ZK after the window has been created. * Retrieves the clinic from the session. * * @param evt - The event object */ public void doAfterCompose(Component window) { try { super.doAfterCompose(window); } catch (Exception e) { log.error("Problem with doAfter Compose: " + e.getMessage(), e); } dob.setConstraint(new MyDateConstraint(doberr, MyDateConstraint.STRICT | MyDateConstraint.SERVER | MyDateConstraint.NO_FUTURE | MyDateConstraint.NO_TODAY)); } }
package com.app.util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.log4j.Logger; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.WrongValueException; import org.zkoss.zul.CustomConstraint; import org.zkoss.zul.Datebox; import org.zkoss.zul.Label; import org.zkoss.zul.SimpleDateConstraint; public class MyDateConstraint extends SimpleDateConstraint implements CustomConstraint { private static final long serialVersionUID = 1L; private static final Logger log = Logger.getLogger(MyDateConstraint.class); private Label errfld; public MyDateConstraint(Label errlbl, int flags) { super(flags); errfld = errlbl; } public MyDateConstraint(Label errlbl, String constraint) { super(constraint); errfld = errlbl; } public MyDateConstraint(Label err, int flags, String errmsg) { super(flags, errmsg); errfld = err; } public MyDateConstraint(Label errlbl, String regex, String errmsg) { super(regex, errmsg); errfld = errlbl; } public MyDateConstraint(Label errlbl, int flags, String regex, String errmsg) { super(flags, regex, errmsg); errfld = errlbl; } public MyDateConstraint(Label errlbl, int flags, java.util.Date begin, java.util.Date end, java.lang.String errmsg) { super(flags, begin, end, errmsg); errfld = errlbl; } @Override public void showCustomError(Component comp, WrongValueException ex) { if (ex != null) { log.debug("WrongValueException Message: " + ex.getMessage()); errfld.setValue(ex.getMessage()); errfld.setVisible(true); } else { log.debug("No exception"); if (comp instanceof Datebox) { log.debug("datebox error message is: " + ((Datebox)comp).getErrorMessage()); } else log.debug("comp is: " + comp.getClass().getName()); errfld.setVisible(false); } } }
Asked: 2011-11-26 07:37:38 +0800
Seen: 1,025 times
Last updated: Nov 29 '11