Hi Ben,

I think I understand your problem. I am have a similar situation where I have a person class with a birthday.

I ended up writing a Converter to handle the date (because I needed it in the format dd/mm/yyyy). I used the Person-conversion.properties to say that I want that converter applied to that field. And for the validation I have something like the following in the Person-validation.xml file.

<field name="birthday">
               <field-validator type="conversion">
<message>${getText("persona.birthday")} : ${getText("validation.invalid")}</message>
               </field-validator>
            </field>

So my converter handles the case where there is no date. I don't have to use the getText method. I just refer to the property as I would with a normal field.

Then my converter throws a conversion exception if there are problems converting the date. I then catch this with the above line in my validation.xml.

I am new to the Struts 2 world but I think this is a relatively clean solution. Happy to hear if anyone else has a better way.

Cheers, Carl.


Quoting ben_979 <benninesevenn...@yahoo.ca>:



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






---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to