I have an object with a java.util.Date field. I present the object inside a
form with the following tag:

<s:textfield key="detail.date"
value="%{getText('detail.date',{schedule.dateTime})}" label="Date/Time"/>

where detail.date is a date formatting pattern.

The problem arises because after getText(), the field is populated with a
String. When the String is submitted, I get an error because I don't have a
setDateTime(String) method.

I've written a data type conversion routine and applied it to the field, and
it works fine as long as the user enters a date string in a valid format. If
an invalid string is entered, the type conversion fails and a null is
returned - so the user doesn't see the original date string, or even the
incorrect one they entered - they see 'null'.

So, I tried to add validation (using a validation xml file). However, it
seems that the conversion is done regardless of whether or not the
validation fails, so I end up with the same results.

Next, I tried to implement the validation using the validate() method in the
class, but I'm having similar troubles - I need to use the converter to
convert the String to a Date - and I end up with all the previously
described problems. If I don't use a converter, the field is null when it
gets to the validate() method. 

It isn't practical for me to change the class that contains the Date field
to add a setDateTime(String) method. It seems like a hack to use a variable
outside of the class to hold the Date in String form and then worry about
keeping it in sync with the actual object.

I can't be the first to struggle with this, so I'd be interested in hearing
how some of you have solved this in the past - is there a clean and elegant
solution?

Thanks in advance!


-- 
View this message in context: 
http://old.nabble.com/Validation-and-conversion-conflict---best-method--tp26341189p26341189.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

Reply via email to