0

controlling error display for a datebox custom constraint

asked 2011-11-26 07:37:38 +0800

mhongslo gravatar image mhongslo
51 1

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>


MyController.java

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));
 
    }
}


MyDateConstraint.java

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);
        }
    }

}


Would greatly appreciate some suggestions or answer to this problem of not being able to obtain the invalid date error message.

delete flag offensive retag edit

1 Reply

Sort by ยป oldest newest

answered 2011-11-29 06:59:44 +0800

vincentjian gravatar image vincentjian
2245 6

Hi,

This is a bug, please refer to the link: http://tracker.zkoss.org/browse/ZK-631

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-11-26 07:37:38 +0800

Seen: 1,025 times

Last updated: Nov 29 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More