You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Borislav Sabev <b....@eventsoft.de> on 2005/07/08 18:17:32 UTC

Validator, sutrts-config and form-property's types

Hi all,

The problem I'd like to discuss is how Validator works with form 
properties. IMHO there is a contradiction (or maybe I don't know how to 
resolve this problem - in this case, please help me) with types of Form 
properties. The end result is that if I want to use the Validator, all 
my form properties MUST be java.lang.String, otherwise Struts (in fact 
BeanUtils and ConvertorUtils) throws 
org.apache.commons.beanutils.ConversionException. So why should specify 
the type of form properties if Validator so and so doesn't work with any 
other type than java.util.String.
To clarify the problem here is an example:
I have this form:
        <form-bean name="testValidation"
            type="org.apache.struts.validator.DynaValidatorForm">
            <form-property name="startDate" type="java.util.Date" />
            <form-property name="endDate" type="java.util.Date" />
        </form-bean>

and here is the snippet of my validation.xml file that should check that 
the dates entered are in some special format:
        <form name="testValidation">
            <field property="startDate"
                    depends="date">
                    <arg key="startDate"/>
                <var>
                <var-name>datePattern</var-name>
                <var-value>yyyy-MM-dd</var-value>
                </var>                   
            </field>
            <field property="endDate"
                    depends="date">
                    <arg key="endDate"/>
                <var>
                <var-name>datePattern</var-name>
                <var-value>yyyy-MM-dd</var-value>
                </var>                   
            </field>
        </form>

The idea behind this is that I want the parameters that are coming with 
the request, to be "translated" to their expected types once and only once!
So if you try this, you will get 
org.apache.commons.beanutils.ConversionException (thrown from 
RequestProcessor if I remember correctly).

As soon as I set this
            <form-property name="startDate" type="java.lang.String" />
            <form-property name="endDate" type="java.util.String" />
the validator starts to work correctly.
So here I see at least 2 problems:
1. I'm forced again to parse the String properties of the form to proper 
java.util.Date objects (or whatever other type it has to be)
2.  because of  1. ,  my date format string now is in 2 places instead 
of one place i.e. I have a support problem

So IMHO this is a "framework" inconsistency since.
Please tell me if I do something wrong or this is one of "known 
limitations".


Borislav


Re: Validator, sutrts-config and form-property's types

Posted by Borislav Sabev <b....@eventsoft.de>.
Craig McClanahan wrote:

>>Is this with JSP by specification or it's a implementation issue? I hope
>>it's by spec ...
>>    
>>
>
>  
>
Sorry, my mistake, I wanted to write JSF.

>It's not really a JSP issue, but a component issue.  In JavaServer
>Faces (or Tapestry), the components themselves maintain the string
>version of the submitted value so that it can be redisplayed, and also
>know how to do the appropriate conversions.  Thus, in a JSF app, the
>data you bind to can be of appropriate native types.
>
>Struts 1.x does not have a user interface component model, so the app
>has to do all this work itself.
>
>Craig
>
>  
>
I think that may be I'm misunderstood or at least I didn't set the 
question properly.
If the Validator works correctly only if I use String types for Form 
properties, in this case why it's possible at all I to specify them as a 
type in Struts-Config file? If every time BeanUtils throws this 
ConversionException, then even population of the forms (BEFORE any 
validation) doesn't work properly.  So even if I don't use the 
validation, population works only if I specify Strings, thus no need at 
all the form property types to be customizable at all.

Borislav



Re: Validator, sutrts-config and form-property's types

Posted by Craig McClanahan <cr...@gmail.com>.
On 7/8/05, Borislav Sabev <b....@eventsoft.de> wrote:
> Jeff Beal wrote:
> 
> >I think that if Mark Galbreath were still around, he'd reply with
> >"Validator sucks" or something similar.
> >
> >Basically, your statements below are all quite correct.  I will add
> >(in response to Point 1 below) that the BeanUtils library is what
> >Struts uses internally to copy values from the request to your
> >ActionForm, and it knows how to convert Strings into other types, like
> >Date.  I typically use BeanUtils to populate a (well typed) DTO from
> >my ActionForm once the data in the form has been validated.
> >
> >
> >
> True, I use it for the same purpose, but my expectations about the forms
> and validations were much higher ... obviously this is a limitation and
> that's it (for now)
> 
> >Just to add a little bit, all of this is because ActionForms (whether
> >DynaForms or not) are populated first, then validated.  If you have a
> >form property with a type other than String, there's always a chance
> >that incoming parameters can't be converted from String to that type.
> >If the incoming request were validated prior to populating the
> >ActionForm, we could have typed properties on ActionForms.  I believe
> >this is approximately the approach taken by JSF.
> >
> >
> Is this with JSP by specification or it's a implementation issue? I hope
> it's by spec ...

It's not really a JSP issue, but a component issue.  In JavaServer
Faces (or Tapestry), the components themselves maintain the string
version of the submitted value so that it can be redisplayed, and also
know how to do the appropriate conversions.  Thus, in a JSF app, the
data you bind to can be of appropriate native types.

Struts 1.x does not have a user interface component model, so the app
has to do all this work itself.

Craig



> 
> Cheers
> Borislav
> 
> 
> 
> ---------------------------------------------------------------------
> 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: Validator, sutrts-config and form-property's types

Posted by Borislav Sabev <b....@eventsoft.de>.
Jeff Beal wrote:

>I think that if Mark Galbreath were still around, he'd reply with
>"Validator sucks" or something similar.
>
>Basically, your statements below are all quite correct.  I will add
>(in response to Point 1 below) that the BeanUtils library is what
>Struts uses internally to copy values from the request to your
>ActionForm, and it knows how to convert Strings into other types, like
>Date.  I typically use BeanUtils to populate a (well typed) DTO from
>my ActionForm once the data in the form has been validated.
>
>  
>
True, I use it for the same purpose, but my expectations about the forms 
and validations were much higher ... obviously this is a limitation and 
that's it (for now)

>Just to add a little bit, all of this is because ActionForms (whether
>DynaForms or not) are populated first, then validated.  If you have a
>form property with a type other than String, there's always a chance
>that incoming parameters can't be converted from String to that type. 
>If the incoming request were validated prior to populating the
>ActionForm, we could have typed properties on ActionForms.  I believe
>this is approximately the approach taken by JSF.
>  
>
Is this with JSP by specification or it's a implementation issue? I hope 
it's by spec ...

Cheers
Borislav


Re: Validator, sutrts-config and form-property's types

Posted by Jeff Beal <jb...@gmail.com>.
I think that if Mark Galbreath were still around, he'd reply with
"Validator sucks" or something similar.

Basically, your statements below are all quite correct.  I will add
(in response to Point 1 below) that the BeanUtils library is what
Struts uses internally to copy values from the request to your
ActionForm, and it knows how to convert Strings into other types, like
Date.  I typically use BeanUtils to populate a (well typed) DTO from
my ActionForm once the data in the form has been validated.

Just to add a little bit, all of this is because ActionForms (whether
DynaForms or not) are populated first, then validated.  If you have a
form property with a type other than String, there's always a chance
that incoming parameters can't be converted from String to that type. 
If the incoming request were validated prior to populating the
ActionForm, we could have typed properties on ActionForms.  I believe
this is approximately the approach taken by JSF.

-- Jeff

On 7/8/05, Borislav Sabev <b....@eventsoft.de> wrote:

> As soon as I set this
>             <form-property name="startDate" type="java.lang.String" />
>             <form-property name="endDate" type="java.util.String" />
> the validator starts to work correctly.
> So here I see at least 2 problems:
> 1. I'm forced again to parse the String properties of the form to proper
> java.util.Date objects (or whatever other type it has to be)
> 2.  because of  1. ,  my date format string now is in 2 places instead
> of one place i.e. I have a support problem
> 
> So IMHO this is a "framework" inconsistency since.
> Please tell me if I do something wrong or this is one of "known
> limitations".
> 
> 
> Borislav

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


Re: Validator, sutrts-config and form-property's types

Posted by Laurie Harper <la...@holoweb.net>.
Actually, BeanUtils can convert from String to a variety of Java types; 
Date just doesn't happen to be one of them by default. However, it's 
generally recommended to use String properties in action forms exclusively 
so that invalid user inputs can be re-displayed as entered.

For example, if you ask for a data and the user enters 'Barney', date 
parsing is going to fail wherever it occurs. If the form uses type Date, 
there's no way you can represent the invalid input 'Barney' in the form, so 
you can't redisplay it to the user for correction. Hence use of String 
valued form properties.

If you don't care about round-tripping bad inputs this way, you can always 
register additional converters with BeanUtils to handle the String to X 
conversions you need.

L.

Borislav Sabev wrote:

> Hi all,
> 
> The problem I'd like to discuss is how Validator works with form 
> properties. IMHO there is a contradiction (or maybe I don't know how to 
> resolve this problem - in this case, please help me) with types of Form 
> properties. The end result is that if I want to use the Validator, all 
> my form properties MUST be java.lang.String, otherwise Struts (in fact 
> BeanUtils and ConvertorUtils) throws 
> org.apache.commons.beanutils.ConversionException. So why should specify 
> the type of form properties if Validator so and so doesn't work with any 
> other type than java.util.String.
> To clarify the problem here is an example:
> I have this form:
>        <form-bean name="testValidation"
>            type="org.apache.struts.validator.DynaValidatorForm">
>            <form-property name="startDate" type="java.util.Date" />
>            <form-property name="endDate" type="java.util.Date" />
>        </form-bean>
> 
> and here is the snippet of my validation.xml file that should check that 
> the dates entered are in some special format:
>        <form name="testValidation">
>            <field property="startDate"
>                    depends="date">
>                    <arg key="startDate"/>
>                <var>
>                <var-name>datePattern</var-name>
>                <var-value>yyyy-MM-dd</var-value>
>                </var>                              </field>
>            <field property="endDate"
>                    depends="date">
>                    <arg key="endDate"/>
>                <var>
>                <var-name>datePattern</var-name>
>                <var-value>yyyy-MM-dd</var-value>
>                </var>                              </field>
>        </form>
> 
> The idea behind this is that I want the parameters that are coming with 
> the request, to be "translated" to their expected types once and only once!
> So if you try this, you will get 
> org.apache.commons.beanutils.ConversionException (thrown from 
> RequestProcessor if I remember correctly).
> 
> As soon as I set this
>            <form-property name="startDate" type="java.lang.String" />
>            <form-property name="endDate" type="java.util.String" />
> the validator starts to work correctly.
> So here I see at least 2 problems:
> 1. I'm forced again to parse the String properties of the form to proper 
> java.util.Date objects (or whatever other type it has to be)
> 2.  because of  1. ,  my date format string now is in 2 places instead 
> of one place i.e. I have a support problem
> 
> So IMHO this is a "framework" inconsistency since.
> Please tell me if I do something wrong or this is one of "known 
> limitations".
> 
> 
> Borislav
> 
> 
> ------------------------------------------------------------------------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org


-- 
Laurie, Open Source advocate, Java geek and novice blogger:
http://www.holoweb.net/~laurie/


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