In our application we have a class, which extends another class.
Simple:
We have a form which contains an input field for field d and d2. So far everything goes well, when we request the page containing the form, we see those fields popup.Code:public class ObjA { private Date d; // Getters and setters } public class ObjB extends ObjA { private ObjC objC // Getters and setters } public class ObjC { private Date d2; // Getters and Setters }
Since you have to enter dates as dd/MM/yyyy, we have a property editor to parse the entered date, corresponding to that pattern, with the SimpleDateFormat-class.
The problem was that the client wanted an error message, when users entered something like 32/12/2009. So far so good, what I did was format the date and check whether or not it was equal to the entered date. So if you add 32/12/2009 and you parse it, something like Jan 01 2010 should come up and validation fails. An exception is thrown (IllegalArgumentException to be more precise).
When you return to the page, the previous entered date is still there. Instead, I want to show 32/12/2009, instead of the previous value.
Now here lies the problem. For some reason, the getAsText() value in my property editor is not called for the field d. d2 is fine, but d somehow doesn't make it.
Does anyone know what the problem could be? setAsText is called correctly and everything is fine. To manipulate the flow, I do a setValue() when validation fails, so that 32/12/2009 is set as value. But that doesn't matter, since the getAsText()-method isn't called.
My Property Editor:
setAsText is correct, but the application doesn't call the getAsText method for the first date. It does for the second and all the others.Code:public class CustomDateEditor extends PropertyEditorSupport { /** Dateformat for the given date */ private SimpleDateFormat dateFormat; /** To allow empty entries */ private boolean allowEmpty; /** To be able to make an entry strict */ private boolean strict; /** Logger for this class */ private Logger logger = Logger.getLogger(CustomDateEditor.class); /** * Constructor * * @param dateFormat * @param allowEmpty * @param strict */ public CustomDateEditor(SimpleDateFormat dateFormat, boolean allowEmpty, boolean strict) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.strict = strict; } /** * What text to show in the input field, when validation fails. * * @param text * @throws IllegalArgumentException */ public void setAsText(String text) throws IllegalArgumentException { if(this.allowEmpty && ! StringUtils.hasText(text)) { setValue(null); } else { String pattern = this.dateFormat.toPattern(); if(strict && pattern.length() != text.length()) { throw new IllegalArgumentException("Error date Pattern: " + text + ", should be " + pattern); } try{ Date dateValue = this.dateFormat.parse(text); if(! this.dateFormat.format(dateValue).equals(text)) { logger.error("Error while parsing date. User entered wrong date: " + text); setValue(text); logger.debug("==========VALUE: " + getValue()); throw new IllegalArgumentException("Could not parse date: " + text); } else { setValue(this.dateFormat.parse(text)); } } catch(ParseException ex) { throw new IllegalArgumentException("Could not parse date: " + ex.getMessage()); } } } /** * Get the value as text, for a given date pattern * * @return text representation of the date */ public String getAsText() { String returnValue = ""; Object o = getValue(); if(o != null) { // get Value and return wrong date if e.g. 40/12/2009 was entered } return returnValue; } }


Reply With Quote