You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Oren Livne <ol...@aoce.utah.edu> on 2007/03/03 16:30:34 UTC

Two Struts validation problems

Dear All:

I have two problems with Validator. I'm using Struts 1.3.5, Tomcat 6.0.9 
and Java 1.6:
1) I have a form property that is an integer. When I set its validation 
rule to "integer", it
has no effect.
- The form field is populated with a zero value even if a letter is 
typed in the form
instead of a number. How do I make sure that a typing a string would 
result in a validation error?
- Should I use Strings for all my form properties or can I use integers? 
Or Integers?
- I am using PropertyUtil.copyProperties(). If I use Strings for all my 
form properties and integers
in my model's data transfer object, would it automatically convert the 
type of variables in this method?

2) A "validwhen" test condition seems to be ignored:
                <var>
                   <var-name>test</var-name>
                   <var-value>((*this* != null) or (newChildForm.comment 
== null))</var-value>
                </var>
This has a reference to a nested property of the form. Is that OK to use 
it in a test condition?

Thanks,
Oren

-- 
======================================================================
Oren Livne, Ph.D.
Research Assistant Professor of Mathematics
RUReady Software Architect

Academic Outreach and Continuing Education
1901 East South Campus Dr., Room 2197-D
University of Utah, Salt Lake City, UT 84112-9399
Tel  : (801) 581-6831     Cell: (801) 631-3885     Fax: (801) 585-5414
Email: olivne@aoce.utah.edu              Web:  http://ruready.net/oren
======================================================================

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


Re: Two Struts validation problems

Posted by Oren Livne <ol...@aoce.utah.edu>.
Dear Ed:

Thanks so much for your response (all other that replied -- thanks so 
much as well!).
For now, I would like to stick to the first solution: ActionForm with 
String properties.
I already have VOs and don't mind copying over from the form to the VO 
and back.
My last question on this issue is: if I use BeanUtils.copyProperties(vo, 
form) where
I have a form.field : String and vo.field : int (same name, different 
type) and have
validated the form so that I know form.field is convertible to an int, 
would copyProperties()
do that for me? Is that what I should be using (and not 
PropertyUtils.copyProperties())?

Thanks so much,
oren

Ed Griebel wrote:
> Chris is right, the zero occurs in the java.lang.Integer(String)
> constructor when it is passed a non-numeric string value. You would
> see this happen in your ActionForm and action class if you weren't
> using the validator. The same thing happens with a non-numeric in a
> java.lang.Double(String) constructor.
>
> For Struts 1.x, the question of Strings vs. native types in formbeans
> comes up about every 6 months, judging from the questions this week it
> seems it's that time again. The general consensus is that Strings
> preserve incorrect input for redisplay and don't do "mysterious"
> conversions like above, but if native types are used then the formbean
> can also be used as a data transfer object for the database layer,
> removing a set of DTO (a.k.a. DO, VO, etc) classes. Of course, using
> LazyDynaBeans and their ilk let you have the best of both worlds :-)
>
> -ed
>
> On 3/3/07, Christopher Schultz <ch...@christopherschultz.net> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Oren,
>>
>> Oren Livne wrote:
>> > Dear Chris:
>> > Yes, other validations are working. For instance, I also have an
>> > "intRange" validation on the same integer field. If I type a letter 
>> for
>> > the integer field, it sets it to 0, displays it as 0, and says it's
>> > outside the permissible range (which happens to be 1-4). No errors on
>> > startup. Any idea what could go wrong? :(
>> >
>> > Should I use a String instead of int in my form for that field?
>>
>> I pretty much always use strings.
>>
>> If you have your field type set to "java.lang.Integer", then its value
>> will be either null of an actual number. It's possible that the
>> converter used by the validator will set an invalid input to new
>> Integer(0) just as a default value. So, for instance, if you enter "foo"
>> in for a number, it comes back "0", when it should have been "foo" (at
>> least, I would expect it to say "foo" and an error message).
>>
>> IMHO, a rejected form should come back to the user in exactly the same
>> way it was submitted (plus error messages, of course).
>>
>> Using strings for all form fields /will/ work, but I'm not entirely sure
>> it's necessary to make that happen. I just know that it works for me.
>>
>> - -chris
>>
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.6 (MingW32)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>>
>> iD8DBQFF6c0b9CaO5/Lv0PARAt6qAJ49qun09ehwDYu0lpva3Cvyy0c7EACglUF2
>> fLHeh3/nKs3iQ2sgPG5XBXA=
>> =jdT/
>> -----END PGP SIGNATURE-----
>>
>> ---------------------------------------------------------------------
>> 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

-- 
======================================================================
Oren Livne, Ph.D.
Research Assistant Professor of Mathematics
RUReady Software Architect

Academic Outreach and Continuing Education
1901 East South Campus Dr., Room 2197-D
University of Utah, Salt Lake City, UT 84112-9399
Tel  : (801) 581-6831     Cell: (801) 631-3885     Fax: (801) 585-5414
Email: olivne@aoce.utah.edu              Web:  http://ruready.net/oren
======================================================================

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


Re: Two Struts validation problems

Posted by Martin Gainty <mg...@hotmail.com>.
concerning the initialisation of those beans which mechanism would take control for bean initialisation when other bean initialisers are configured
(Im thinking of hibernate bean child set and or spring bean property)?

Thanks,
M-
--------------------------------------------------------------------------- 
This e-mail message (including attachments, if any) is intended for the use of the individual or entity to which it is addressed and may contain information that is privileged, proprietary , confidential and exempt from disclosure. If you are not the intended recipient, you are notified that any dissemination, distribution or copying of this communication is strictly prohibited.
--------------------------------------------------------------------------- 
Le présent message électronique (y compris les pièces qui y sont annexées, le cas échéant) s'adresse au destinataire indiqué et peut contenir des renseignements de caractère privé ou confidentiel. Si vous n'êtes pas le destinataire de ce document, nous vous signalons qu'il est strictement interdit de le diffuser, de le distribuer ou de le reproduire.
----- Original Message ----- 
From: "Ed Griebel" <ed...@gmail.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Saturday, March 03, 2007 6:50 PM
Subject: Re: Two Struts validation problems


> Chris is right, the zero occurs in the java.lang.Integer(String)
> constructor when it is passed a non-numeric string value. You would
> see this happen in your ActionForm and action class if you weren't
> using the validator. The same thing happens with a non-numeric in a
> java.lang.Double(String) constructor.
> 
> For Struts 1.x, the question of Strings vs. native types in formbeans
> comes up about every 6 months, judging from the questions this week it
> seems it's that time again. The general consensus is that Strings
> preserve incorrect input for redisplay and don't do "mysterious"
> conversions like above, but if native types are used then the formbean
> can also be used as a data transfer object for the database layer,
> removing a set of DTO (a.k.a. DO, VO, etc) classes. Of course, using
> LazyDynaBeans and their ilk let you have the best of both worlds :-)
> 
> -ed
> 
> On 3/3/07, Christopher Schultz <ch...@christopherschultz.net> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Oren,
>>
>> Oren Livne wrote:
>> > Dear Chris:
>> > Yes, other validations are working. For instance, I also have an
>> > "intRange" validation on the same integer field. If I type a letter for
>> > the integer field, it sets it to 0, displays it as 0, and says it's
>> > outside the permissible range (which happens to be 1-4). No errors on
>> > startup. Any idea what could go wrong? :(
>> >
>> > Should I use a String instead of int in my form for that field?
>>
>> I pretty much always use strings.
>>
>> If you have your field type set to "java.lang.Integer", then its value
>> will be either null of an actual number. It's possible that the
>> converter used by the validator will set an invalid input to new
>> Integer(0) just as a default value. So, for instance, if you enter "foo"
>> in for a number, it comes back "0", when it should have been "foo" (at
>> least, I would expect it to say "foo" and an error message).
>>
>> IMHO, a rejected form should come back to the user in exactly the same
>> way it was submitted (plus error messages, of course).
>>
>> Using strings for all form fields /will/ work, but I'm not entirely sure
>> it's necessary to make that happen. I just know that it works for me.
>>
>> - -chris
>>
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.6 (MingW32)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>>
>> iD8DBQFF6c0b9CaO5/Lv0PARAt6qAJ49qun09ehwDYu0lpva3Cvyy0c7EACglUF2
>> fLHeh3/nKs3iQ2sgPG5XBXA=
>> =jdT/
>> -----END PGP SIGNATURE-----
>>
>> ---------------------------------------------------------------------
>> 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: Two Struts validation problems

Posted by Ed Griebel <ed...@gmail.com>.
Chris is right, the zero occurs in the java.lang.Integer(String)
constructor when it is passed a non-numeric string value. You would
see this happen in your ActionForm and action class if you weren't
using the validator. The same thing happens with a non-numeric in a
java.lang.Double(String) constructor.

For Struts 1.x, the question of Strings vs. native types in formbeans
comes up about every 6 months, judging from the questions this week it
seems it's that time again. The general consensus is that Strings
preserve incorrect input for redisplay and don't do "mysterious"
conversions like above, but if native types are used then the formbean
can also be used as a data transfer object for the database layer,
removing a set of DTO (a.k.a. DO, VO, etc) classes. Of course, using
LazyDynaBeans and their ilk let you have the best of both worlds :-)

-ed

On 3/3/07, Christopher Schultz <ch...@christopherschultz.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Oren,
>
> Oren Livne wrote:
> > Dear Chris:
> > Yes, other validations are working. For instance, I also have an
> > "intRange" validation on the same integer field. If I type a letter for
> > the integer field, it sets it to 0, displays it as 0, and says it's
> > outside the permissible range (which happens to be 1-4). No errors on
> > startup. Any idea what could go wrong? :(
> >
> > Should I use a String instead of int in my form for that field?
>
> I pretty much always use strings.
>
> If you have your field type set to "java.lang.Integer", then its value
> will be either null of an actual number. It's possible that the
> converter used by the validator will set an invalid input to new
> Integer(0) just as a default value. So, for instance, if you enter "foo"
> in for a number, it comes back "0", when it should have been "foo" (at
> least, I would expect it to say "foo" and an error message).
>
> IMHO, a rejected form should come back to the user in exactly the same
> way it was submitted (plus error messages, of course).
>
> Using strings for all form fields /will/ work, but I'm not entirely sure
> it's necessary to make that happen. I just know that it works for me.
>
> - -chris
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFF6c0b9CaO5/Lv0PARAt6qAJ49qun09ehwDYu0lpva3Cvyy0c7EACglUF2
> fLHeh3/nKs3iQ2sgPG5XBXA=
> =jdT/
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> 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: Two Struts validation problems

Posted by Oren Livne <ol...@aoce.utah.edu>.

Oren Livne wrote:
> P.S. An important fact I forgot to mention: my VO is persisted to a 
> database using Hibernate. Hibernate wraps this
> object with its own type. Is it possible that the type conversion 
> (String -> int) is not working properly because
> Hibernate's wrapper gets in the way? I did a simple test of copying a 
> String -> int using BeanUtil.copyProperties(),
> and it worked fine, converting "1" to 1 and "a" to 0. If the problem 
> is Hibernate, what can be done about it? I don't
> think I have control over this wrapper, do I?
>
> Thanks again, guys. I'm stuck and you're tremendously helping me.
> Oren
>
> Oren Livne wrote:
>> Dear Laurie & Christopher:
>>
>> Great! Thanks. I'm using Strings now in my form. But as I wrote to 
>> Ed, suppose I validated the form and would like to copy all 
>> properties to a VO (value object) / DTO with one call (e.g. 
>> copyProperties()). How do I get it to automatically convert my form 
>> String property into a VO's corresponding property which is an int or 
>> an Integer?
>>
>> Thanks again
>> Oren
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>

-- 
======================================================================
Oren Livne, Ph.D.
Research Assistant Professor of Mathematics
RUReady Software Architect

Academic Outreach and Continuing Education
1901 East South Campus Dr., Room 2197-D
University of Utah, Salt Lake City, UT 84112-9399
Tel  : (801) 581-6831     Cell: (801) 631-3885     Fax: (801) 585-5414
Email: olivne@aoce.utah.edu              Web:  http://ruready.net/oren
======================================================================

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


Re: Two Struts validation problems

Posted by Oren Livne <ol...@aoce.utah.edu>.
P.S. An important fact I forgot to mention: my VO is persisted to a 
database using Hibernate. Hibernate wraps this
object with its own type. Is it possible that the type conversion 
(String -> int) is not working properly because
Hibernate's wrapper gets in the way? I did a simple test of copying a 
String -> int using BeanUtil.copyProperties(),
and it worked fine, converting "1" to 1 and "a" to 0. If the problem is 
Hibernate, what can be done about it? I don't
think I have control over this wrapper, do I?

Thanks again, guys. I'm stuck and you're tremendously helping me.
Oren

Oren Livne wrote:
> Dear Laurie & Christopher:
>
> Great! Thanks. I'm using Strings now in my form. But as I wrote to Ed, 
> suppose I validated the form and would like to copy all properties to 
> a VO (value object) / DTO with one call (e.g. copyProperties()). How 
> do I get it to automatically convert my form String property into a 
> VO's corresponding property which is an int or an Integer?
>
> Thanks again
> Oren
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

-- 
======================================================================
Oren Livne, Ph.D.
Research Assistant Professor of Mathematics
RUReady Software Architect

Academic Outreach and Continuing Education
1901 East South Campus Dr., Room 2197-D
University of Utah, Salt Lake City, UT 84112-9399
Tel  : (801) 581-6831     Cell: (801) 631-3885     Fax: (801) 585-5414
Email: olivne@aoce.utah.edu              Web:  http://ruready.net/oren
======================================================================

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


Re: Two Struts validation problems

Posted by Oren Livne <ol...@aoce.utah.edu>.
Dear Laurie & Christopher:

Great! Thanks. I'm using Strings now in my form. But as I wrote to Ed, 
suppose I validated the form and would like to copy all properties to a 
VO (value object) / DTO with one call (e.g. copyProperties()). How do I 
get it to automatically convert my form String property into a VO's 
corresponding property which is an int or an Integer?

Thanks again
Oren

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


Re: Two Struts validation problems

Posted by Laurie Harper <la...@holoweb.net>.
Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> Oren,
> 
> Oren Livne wrote:
>> Dear Chris:
>> Yes, other validations are working. For instance, I also have an
>> "intRange" validation on the same integer field. If I type a letter for
>> the integer field, it sets it to 0, displays it as 0, and says it's
>> outside the permissible range (which happens to be 1-4). No errors on
>> startup. Any idea what could go wrong? :(
>>
>> Should I use a String instead of int in my form for that field?
> 
> I pretty much always use strings.
> 
> If you have your field type set to "java.lang.Integer", then its value
> will be either null of an actual number. It's possible that the
> converter used by the validator will set an invalid input to new
> Integer(0) just as a default value. So, for instance, if you enter "foo"
> in for a number, it comes back "0", when it should have been "foo" (at
> least, I would expect it to say "foo" and an error message).
> 
> IMHO, a rejected form should come back to the user in exactly the same
> way it was submitted (plus error messages, of course).
> 
> Using strings for all form fields /will/ work, but I'm not entirely sure
> it's necessary to make that happen. I just know that it works for me.

Chris pretty much nailed it. The default conversion behaviour for an int 
property is to default it to 0 when conversion fails. For an Integer 
property, you can configure it to use null as a default instead, but 
normally it uses Integer.ZERO to be consistent with the primitive type case.

That's why the 'integer' validation rule always passes: you always have 
a valid integer value, since the conversion default is applied before 
validation happens.

In general, you should always use String type properties. That's the 
only way you can capture and re-display arbitrary user input, and the 
only way you can guarantee that validation is being applied to the 
submitted value, rather than some result of conversion.

L.


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


Re: Two Struts validation problems

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oren,

Oren Livne wrote:
> Dear Chris:
> Yes, other validations are working. For instance, I also have an
> "intRange" validation on the same integer field. If I type a letter for
> the integer field, it sets it to 0, displays it as 0, and says it's
> outside the permissible range (which happens to be 1-4). No errors on
> startup. Any idea what could go wrong? :(
> 
> Should I use a String instead of int in my form for that field?

I pretty much always use strings.

If you have your field type set to "java.lang.Integer", then its value
will be either null of an actual number. It's possible that the
converter used by the validator will set an invalid input to new
Integer(0) just as a default value. So, for instance, if you enter "foo"
in for a number, it comes back "0", when it should have been "foo" (at
least, I would expect it to say "foo" and an error message).

IMHO, a rejected form should come back to the user in exactly the same
way it was submitted (plus error messages, of course).

Using strings for all form fields /will/ work, but I'm not entirely sure
it's necessary to make that happen. I just know that it works for me.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF6c0b9CaO5/Lv0PARAt6qAJ49qun09ehwDYu0lpva3Cvyy0c7EACglUF2
fLHeh3/nKs3iQ2sgPG5XBXA=
=jdT/
-----END PGP SIGNATURE-----

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


Re: Two Struts validation problems

Posted by Oren Livne <ol...@aoce.utah.edu>.
Dear Chris:
Yes, other validations are working. For instance, I also have an 
"intRange" validation on the same integer field. If I type a letter for 
the integer field, it sets it to 0, displays it as 0, and says it's 
outside the permissible range (which happens to be 1-4). No errors on 
startup. Any idea what could go wrong? :(

Should I use a String instead of int in my form for that field?

Thanks
Oren


Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Oren,
>
> Oren Livne wrote:
>   
>> 1) I have a form property that is an integer. When I set its validation
>> rule to "integer", it has no effect.
>>     
>
> [snip]
>   
>> 2) A "validwhen" test condition seems to be ignored:
>>     
>
> Are other validations working? It's possible that you have a syntax
> error in your validation XML config which pretty much breaks all
> validations for that file. Check your catalina.out to see if there are
> any errors on startup. I've had this problem in the past and it often
> looks like the validator is just not following directions.
>
> - -chris
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQFF6b/99CaO5/Lv0PARAvUdAJoDHhKN342QnT9p4CWQiak4QdXWigCdFQHU
> sQbD5Gz6bJDOmylp9TTqL98=
> =9K6y
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>   

-- 
======================================================================
Oren Livne, Ph.D.
Research Assistant Professor of Mathematics
RUReady Software Architect

Academic Outreach and Continuing Education
1901 East South Campus Dr., Room 2197-D
University of Utah, Salt Lake City, UT 84112-9399
Tel  : (801) 581-6831     Cell: (801) 631-3885     Fax: (801) 585-5414
Email: olivne@aoce.utah.edu              Web:  http://ruready.net/oren
======================================================================

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


Re: Two Struts validation problems

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Oren,

Oren Livne wrote:
> 1) I have a form property that is an integer. When I set its validation
> rule to "integer", it has no effect.

[snip]
> 2) A "validwhen" test condition seems to be ignored:

Are other validations working? It's possible that you have a syntax
error in your validation XML config which pretty much breaks all
validations for that file. Check your catalina.out to see if there are
any errors on startup. I've had this problem in the past and it often
looks like the validator is just not following directions.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF6b/99CaO5/Lv0PARAvUdAJoDHhKN342QnT9p4CWQiak4QdXWigCdFQHU
sQbD5Gz6bJDOmylp9TTqL98=
=9K6y
-----END PGP SIGNATURE-----

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