Hi Ben,
Are you throwing a
com.opensymphony.xwork2.conversion.TypeConversionException exception
in your converter class when there is a problem converting the date?
I have the following for my converter class:
public class DateConverter extends StrutsTypeConverter {
private static Logger logger = Logger.getLogger(DateConverter.class);
private static SimpleDateFormat sdf = new SimpleDateFormat();
static {
sdf.applyPattern("dd/MM/yyyy");
sdf.setLenient(false);
}
public Object convertFromString(Map context, String[] values,
Class toClass) {
if (values != null && values.length == 1) {
if (values[0] != null && !values[0].equals("")) {
Pattern p =
Pattern.compile("[0-9]{2}/[0-9]{2}/[0-9]{4}");
Matcher m = p.matcher(values[0]);
boolean b = m.matches();
if (!b)
throw new TypeConversionException("Invalid date
format.");
try {
logger.debug("Parsing " + values[0] + " to a
date.");
Date date = sdf.parse(values[0]);
return date;
} catch (ParseException e) {
e.printStackTrace();
throw new TypeConversionException(e);
}
}
}
return null;
}
public String convertToString(Map context, Object o) {
if (o != null && o instanceof Date) {
return sdf.format(o);
}
return "";
}
}
And then you need to point your converter at the field as per the
documentation.
Cheers,
Carl.
Quoting ben_979 <benninesevenn...@yahoo.ca>:
Carl, if I could bother you for a few more details?
I like your solution, it seems elegant (to me at least!). I'm trying to
implement it, but I'm not getting the results that you are.
I've set up the type converter, and I've verified that it is being invoked.
When my conversion fails (for example, I just type random strings into the
date field of my form), the converter is invoked, and the date conversion
fails (which is correct). However, when I look in the logs, I see that it is
trying to invoke a set method for my date field with a String value as the
parameter ( setDateTime(String) ) which I don't have, because dateTime is
expected to be of type Date.
If I enter a date in the proper format, everything works as expected.
From the jsp:
<s:textfield name="schedule.dateTime"
value="%{getText('detail.date',{schedule.dateTime})}" label="Date/Time"/>
//detail.date is for formatting
From the validation.xml
<field name="schedule.dateTime">
<field-validator type="conversion" short-circuit="true">
<message> Try another format </message>
</field-validator>
</field>
Thanks in advance, if you have a chance to respond.
--
View this message in context:
http://old.nabble.com/Validation-and-conversion-conflict---best-method--tp26341189p26545367.html
Sent from the Struts - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org