You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Michael Kastner <ka...@galt.de> on 2004/04/26 16:19:14 UTC

date validation

Hello,

maybe someone can help me. I am trying to use DynaValidatorForm to enter 
(amongst other fields) a date field.

So, my form bean definition contains this property setting:

<form-property name="paymentDue" type="java.util.Date" />

In my validation defintion, I use

<field
     property="paymentDue"
     depends="required,date">
     <msg
         name="required"
         key="invoiceeditform.error.paymentdue.required"/>
     <var>
         <var-name>datePattern</var-name>
         <var-value>dd.MM.yyyy</var-value>
     </var>
</field>

However, when I send the form, this exception is being thrown:

org.apache.commons.beanutils.ConversionException: Cannot assign value of 
type 'java.lang.String' to property 'paymentDue' of type java.util.Date'
...
...


Can anyone give me a hint, if there's anything wrong with my settings? 
I've checked with similar questions in this list and some 
documentations. If I am not totally off track the validator should be 
able to read date strings and convert them to java.util.Date objects.

Any help is very much appreciated.


Michael Kastner


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


Re: date validation

Posted by Melissa L Kelley <st...@stuology.net>.
On Mon, 26 Apr 2004, Michael Kastner wrote:

> Hello,
>
> maybe someone can help me. I am trying to use DynaValidatorForm to enter
> (amongst other fields) a date field.
>
> So, my form bean definition contains this property setting:
>
> <form-property name="paymentDue" type="java.util.Date" />
>

Change this to

<form-property name="paymentDue" type="java.lang.String" />

You can read here for all the supported types (java.util.Date isn't one of
them): http://jakarta.apache.org/struts/userGuide/building_controller.html



------------------------------
Melissa L Kelley
www.stuology.net
------------------------------


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


Re: date validation

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
The real issue (IMO) here is that "conversion" is the first step in
validation and you have to identify the date format to the validator (which
handles internationalization) - but struts/validator then does nothing with
the results of that conversion - ending up either with Michaels scenario
where his form bean is defined as a date type and BeanUtils has to duplicate
what the validator is doing or with the "String" properties alternative, and
then you end up having to re-convert the value in the Action again.

Its a shame that those "validator" conversions (not just for date types) are
just thrown away and end up having to be duplicated - whatever the solution
chosen.

Niall

----- Original Message ----- 
From: "Joe Germuska" <Jo...@Germuska.com>
Sent: Monday, April 26, 2004 3:53 PM


> This is actually a fairly complicated question.  All of Struts
> automatic bean population relies implicitly on the commons-beanutils
> package (http://jakarta.apache.org/commons/beanutils/)  Ultimately,
> this is really a question about that library, but obviously it's
> important to Struts users, so there's no reason to shuffle you off to
> commons-dev for an answer.
>
> This is mostly invisible, because BeanUtils comes with standard
> converters for most types.  However, since there isn't a single
> universal String format for dates, there can be no pre-registered
> date converter.
>
> So, in general, the solution is to register a converter that knows
> the string format.  Originally, "registration" was a static method on
> the ConvertUtils class
>
(http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutil
s/ConvertUtils.html)
> mostly because everything on ConvertUtils (and in the BeanUtils
> package in general) were implemented as static methods.  It was later
> realized that this might not be suitably flexible, and bean-instance
> forms were created.  However, Struts has never been changed to use
> this alternate syntax.
>
> So by this point, you might agree that the simplest answer is tomake
> the form property type "String" instead of "Date", and do the
> conversion inside your Action before passing it in to your
> application (model layer).  Alternatively, you could write a FormBean
> class (instead of using DynaValidatorForm) which had a String setter
> for the property but an alternate Date getter.
>
> The biggest problem with doing this in a framework level way is that
> even within a single application, you probably can't commit to a
> single String format for dates -- what if sometimes you need people
> to enter times, and other times dates, and other times both?  Also,
> depending on your design principles, it may not be acceptable to have
> even a single date format for an internationalized application.
>
> If you really had a single input date format, then you might just
> write a Struts PlugIn that looked like this (warning, pseudocode)
>
> public class RegisterDateFormatPlugIn implements PlugIn {
>
>    public void setDateFormat(String string) {...}
>    public String getDateFormat() {...}
>
>    public void init(ActionServlet servlet, ModuleConfig config)
> {
>      ConvertUtils.register(new MyDateConverter(this.getDateFormat()),
> java.util.Date.class);
> }
>
>   private static class MyDateConverter implements Converter
> {
>    private MyDateConverter(String dateFormat)
>   {
>   }
>    // simple implementation of o.a.c.beanutils.Converter interface...
> }
>
> }
>
> That would solve it in a general way for all String <->
> java.util.Date conversions done by BeanUtils in Struts.
>
> Joe
>
>
>
>
>
> At 4:19 PM +0200 4/26/04, Michael Kastner wrote:
> >Hello,
> >
> >maybe someone can help me. I am trying to use DynaValidatorForm to
> >enter (amongst other fields) a date field.
> >
> >So, my form bean definition contains this property setting:
> >
> ><form-property name="paymentDue" type="java.util.Date" />
> >
> >In my validation defintion, I use
> >
> ><field
> >     property="paymentDue"
> >     depends="required,date">
> >     <msg
> >         name="required"
> >         key="invoiceeditform.error.paymentdue.required"/>
> >     <var>
> >         <var-name>datePattern</var-name>
> >         <var-value>dd.MM.yyyy</var-value>
> >     </var>
> ></field>
> >
> >However, when I send the form, this exception is being thrown:
> >
> >org.apache.commons.beanutils.ConversionException: Cannot assign
> >value of type 'java.lang.String' to property 'paymentDue' of type
> >java.util.Date'
> >...
> >...
> >
> >
> >Can anyone give me a hint, if there's anything wrong with my
> >settings? I've checked with similar questions in this list and some
> >documentations. If I am not totally off track the validator should
> >be able to read date strings and convert them to java.util.Date
> >objects.
> >
> >Any help is very much appreciated.
>
>
> -- 
> Joe Germuska
> Joe@Germuska.com
> http://blog.germuska.com
>        "Imagine if every Thursday your shoes exploded if you tied them
> the usual way.  This happens to us all the time with computers, and
> nobody thinks of complaining."
>              -- Jef Raskin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
>



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


Re: date validation

Posted by Joe Germuska <Jo...@Germuska.com>.
This is actually a fairly complicated question.  All of Struts 
automatic bean population relies implicitly on the commons-beanutils 
package (http://jakarta.apache.org/commons/beanutils/)  Ultimately, 
this is really a question about that library, but obviously it's 
important to Struts users, so there's no reason to shuffle you off to 
commons-dev for an answer.

This is mostly invisible, because BeanUtils comes with standard 
converters for most types.  However, since there isn't a single 
universal String format for dates, there can be no pre-registered 
date converter.

So, in general, the solution is to register a converter that knows 
the string format.  Originally, "registration" was a static method on 
the ConvertUtils class 
(http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/ConvertUtils.html) 
mostly because everything on ConvertUtils (and in the BeanUtils 
package in general) were implemented as static methods.  It was later 
realized that this might not be suitably flexible, and bean-instance 
forms were created.  However, Struts has never been changed to use 
this alternate syntax.

So by this point, you might agree that the simplest answer is tomake 
the form property type "String" instead of "Date", and do the 
conversion inside your Action before passing it in to your 
application (model layer).  Alternatively, you could write a FormBean 
class (instead of using DynaValidatorForm) which had a String setter 
for the property but an alternate Date getter.

The biggest problem with doing this in a framework level way is that 
even within a single application, you probably can't commit to a 
single String format for dates -- what if sometimes you need people 
to enter times, and other times dates, and other times both?  Also, 
depending on your design principles, it may not be acceptable to have 
even a single date format for an internationalized application.

If you really had a single input date format, then you might just 
write a Struts PlugIn that looked like this (warning, pseudocode)

public class RegisterDateFormatPlugIn implements PlugIn {

   public void setDateFormat(String string) {...}
   public String getDateFormat() {...}

   public void init(ActionServlet servlet, ModuleConfig config)
{
     ConvertUtils.register(new MyDateConverter(this.getDateFormat()), 
java.util.Date.class);
}

  private static class MyDateConverter implements Converter
{
   private MyDateConverter(String dateFormat)
  {
  }
   // simple implementation of o.a.c.beanutils.Converter interface...
}

}

That would solve it in a general way for all String <-> 
java.util.Date conversions done by BeanUtils in Struts.

Joe





At 4:19 PM +0200 4/26/04, Michael Kastner wrote:
>Hello,
>
>maybe someone can help me. I am trying to use DynaValidatorForm to 
>enter (amongst other fields) a date field.
>
>So, my form bean definition contains this property setting:
>
><form-property name="paymentDue" type="java.util.Date" />
>
>In my validation defintion, I use
>
><field
>     property="paymentDue"
>     depends="required,date">
>     <msg
>         name="required"
>         key="invoiceeditform.error.paymentdue.required"/>
>     <var>
>         <var-name>datePattern</var-name>
>         <var-value>dd.MM.yyyy</var-value>
>     </var>
></field>
>
>However, when I send the form, this exception is being thrown:
>
>org.apache.commons.beanutils.ConversionException: Cannot assign 
>value of type 'java.lang.String' to property 'paymentDue' of type 
>java.util.Date'
>...
>...
>
>
>Can anyone give me a hint, if there's anything wrong with my 
>settings? I've checked with similar questions in this list and some 
>documentations. If I am not totally off track the validator should 
>be able to read date strings and convert them to java.util.Date 
>objects.
>
>Any help is very much appreciated.


-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
       "Imagine if every Thursday your shoes exploded if you tied them 
the usual way.  This happens to us all the time with computers, and 
nobody thinks of complaining."
             -- Jef Raskin

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


Re: date validation

Posted by Riyad Kalla <rs...@email.arizona.edu>.
Hmm good point. Now I'm interested what the solution is too ;)

Michael Kastner wrote:

> Hello Riyad,
>
> thanks for your reply. I've tried it - it still throws the same 
> exception. As far as I understand the documentation, if the entered 
> field data is not recognized as a date, an errormessage should a 
> appear in the messages section. But there shouldn't appear an exeption.
>
> Michael Kastner
>
> Riyad Kalla schrieb:
>
>> Just a shot in the dark until someone that knows replies...
>>
>> The date.valueOf method requires the format "yyyy-mm-dd", but you are 
>> trying to insert "dd.MM.yyyy", maybe try and change your format (jsut 
>> for testing sake) and see if its able to convert it without an 
>> exception.
>>
>> Michael Kastner wrote:
>>
>>> Hello,
>>>
>>> maybe someone can help me. I am trying to use DynaValidatorForm to 
>>> enter (amongst other fields) a date field.
>>>
>>> So, my form bean definition contains this property setting:
>>>
>>> <form-property name="paymentDue" type="java.util.Date" />
>>>
>>> In my validation defintion, I use
>>>
>>> <field
>>>     property="paymentDue"
>>>     depends="required,date">
>>>     <msg
>>>         name="required"
>>>         key="invoiceeditform.error.paymentdue.required"/>
>>>     <var>
>>>         <var-name>datePattern</var-name>
>>>         <var-value>dd.MM.yyyy</var-value>
>>>     </var>
>>> </field>
>>>
>>> However, when I send the form, this exception is being thrown:
>>>
>>> org.apache.commons.beanutils.ConversionException: Cannot assign 
>>> value of type 'java.lang.String' to property 'paymentDue' of type 
>>> java.util.Date'
>>> ...
>>> ...
>>>
>>>
>>> Can anyone give me a hint, if there's anything wrong with my 
>>> settings? I've checked with similar questions in this list and some 
>>> documentations. If I am not totally off track the validator should 
>>> be able to read date strings and convert them to java.util.Date 
>>> objects.
>>>
>>> Any help is very much appreciated.
>>>
>>>
>>> Michael Kastner
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>>> For additional commands, e-mail: user-help@struts.apache.org
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

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


Re: date validation

Posted by Michael Kastner <ka...@galt.de>.
Hello Riyad,

thanks for your reply. I've tried it - it still throws the same 
exception. As far as I understand the documentation, if the entered 
field data is not recognized as a date, an errormessage should a appear 
in the messages section. But there shouldn't appear an exeption.

Michael Kastner

Riyad Kalla schrieb:
> Just a shot in the dark until someone that knows replies...
> 
> The date.valueOf method requires the format "yyyy-mm-dd", but you are 
> trying to insert "dd.MM.yyyy", maybe try and change your format (jsut 
> for testing sake) and see if its able to convert it without an exception.
> 
> Michael Kastner wrote:
> 
>> Hello,
>>
>> maybe someone can help me. I am trying to use DynaValidatorForm to 
>> enter (amongst other fields) a date field.
>>
>> So, my form bean definition contains this property setting:
>>
>> <form-property name="paymentDue" type="java.util.Date" />
>>
>> In my validation defintion, I use
>>
>> <field
>>     property="paymentDue"
>>     depends="required,date">
>>     <msg
>>         name="required"
>>         key="invoiceeditform.error.paymentdue.required"/>
>>     <var>
>>         <var-name>datePattern</var-name>
>>         <var-value>dd.MM.yyyy</var-value>
>>     </var>
>> </field>
>>
>> However, when I send the form, this exception is being thrown:
>>
>> org.apache.commons.beanutils.ConversionException: Cannot assign value 
>> of type 'java.lang.String' to property 'paymentDue' of type 
>> java.util.Date'
>> ...
>> ...
>>
>>
>> Can anyone give me a hint, if there's anything wrong with my settings? 
>> I've checked with similar questions in this list and some 
>> documentations. If I am not totally off track the validator should be 
>> able to read date strings and convert them to java.util.Date objects.
>>
>> Any help is very much appreciated.
>>
>>
>> Michael Kastner
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 



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


Re: date validation

Posted by Riyad Kalla <rs...@email.arizona.edu>.
Just a shot in the dark until someone that knows replies...

The date.valueOf method requires the format "yyyy-mm-dd", but you are 
trying to insert "dd.MM.yyyy", maybe try and change your format (jsut 
for testing sake) and see if its able to convert it without an exception.

Michael Kastner wrote:

> Hello,
>
> maybe someone can help me. I am trying to use DynaValidatorForm to 
> enter (amongst other fields) a date field.
>
> So, my form bean definition contains this property setting:
>
> <form-property name="paymentDue" type="java.util.Date" />
>
> In my validation defintion, I use
>
> <field
>     property="paymentDue"
>     depends="required,date">
>     <msg
>         name="required"
>         key="invoiceeditform.error.paymentdue.required"/>
>     <var>
>         <var-name>datePattern</var-name>
>         <var-value>dd.MM.yyyy</var-value>
>     </var>
> </field>
>
> However, when I send the form, this exception is being thrown:
>
> org.apache.commons.beanutils.ConversionException: Cannot assign value 
> of type 'java.lang.String' to property 'paymentDue' of type 
> java.util.Date'
> ...
> ...
>
>
> Can anyone give me a hint, if there's anything wrong with my settings? 
> I've checked with similar questions in this list and some 
> documentations. If I am not totally off track the validator should be 
> able to read date strings and convert them to java.util.Date objects.
>
> Any help is very much appreciated.
>
>
> Michael Kastner
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>

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