|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
Validation and conversion conflict - best method?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! |
|
|
Re: Validation and conversion conflict - best method?One quick solution might be , which I am using but most of them may not agree with me,
-- Delcare a string in the action which take the value enter in the text box. -- Add the validation in the validation.xml as a regular expression and can add as "requiredString" , if the filed is mandatory to enter. -- The regular expression will check the format of the date. -- once validation passes can convert the string to date throught the util. Its an alternate solution. - Siddiq.
|
|
|
Re: Validation and conversion conflict - best method?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 <benninesevennine@...>: > > > 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-unsubscribe@... > For additional commands, e-mail: user-help@... > > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
|
|
Re: Validation and conversion conflict - best method?I have a related question, but it's a bit of a side-track as I try to solve my original problem. What is the naming convention and the difference between the two types of -validation.xml file naming? I think I understand that ActionClass-validation.xml is called for all (non-excluded) methods in the Action. The documentation on the other method is very scarce and difficult to understand. It suggests that the name should be ActionClass-ActionAlias-validation.xml, however it isn't really clear what "ActionAlias" really means. In my case, my action class is called ScheduleDetail. The form that invokes the update is coded as follows : <s:form action="ScheduleDetail_update"> So, my understanding is that if I want validation ONLY for this method, the naming convention should be: ScheduleDetail-ScheduleDetail_update-validation.xml Is this correct? I've tried ScheduleDetail-validation.xml just to use the basic naming definition, but it doesn't seem to get invoked. The messages that I have defined in the file are never presented, I am seeing what I think are generic struts error messages for type conversion errors (I'm trying to force failure by entering a string in an int field). |
|
|
Re: Validation and conversion conflict - best method?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. |
|
|
Re: Validation and conversion conflict - best method?Normally values go from JSP as String. Parse it using Date Formatter and
convert it to Date yourself :) I have very interesting experiences with this. On Sat, Nov 28, 2009 at 12:05 AM, ben_979 <benninesevennine@...> wrote: > > 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-unsubscribe@... > For additional commands, e-mail: user-help@... > > -- Saeed Iqbal http://www.iqbalconsulting.com Struts - J2EE - Application Architect / Developer |
|
|
Re: Validation and conversion conflict - best method?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 <benninesevennine@...>: > > 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-unsubscribe@... > For additional commands, e-mail: user-help@... > > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@... For additional commands, e-mail: user-help@... |
| Free embeddable forum powered by Nabble | Forum Help |