You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by aris <ar...@agilemovement.it> on 2004/12/09 18:48:54 UTC

Problem with validation (?)

Hi all,

I've a strange behaviour with my application and I suppose the reason is the
ValidatorPlugIn.

I've an html form with an hidden field and the pre-population lets its
value="" (this is right because the value of the field in the associated
ValidatorForm is null).
When I submit the form, after the validation process I have value="0" (the
field is an Integer) but I haven't set validation rules on this field :|

In my opinion the validator plugin uses a default value read somewhere to
fill the value of the hidden field (but I didn't set anything !?!)

Can anyone help me?

Thanks,
aris



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


Re: Problem with validation (?)

Posted by aris <ar...@agilemovement.it>.
Hi all,
I solved my problem!!!! ...reading the documentation!!!!

At page http://struts.apache.org/userGuide/building_view.html
at section "3.3.5 The Struts Validator" I read:
[...]
Note: If your required form property is one of the Java object
representations of primitive types (ie. java.lang.Integer), you must set the
ActionServlet's convertNull init. parameter to true. Failing to do this will
result in the required validation not being performed on that field because
it will default to 0.
[...]

So I followed the note and I modified my web.xml in this way:
<servlet>
   <servlet-name>action</servlet-name>
   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
   <init-param>
      <param-name>convertNull</param-name>
      <param-value>true</param-value>
   </init-param>
   ...
</servlet>

And all works fine!!!!! It was so simple!

Sorry for my worthless question :(

Bye,
aris




----- Original Message ----- 
From: "aris" <ar...@agilemovement.it>
To: <us...@struts.apache.org>
Sent: Thursday, December 09, 2004 6:48 PM
Subject: Problem with validation (?)


> Hi all,
>
> I've a strange behaviour with my application and I suppose the reason is
the
> ValidatorPlugIn.
>
> I've an html form with an hidden field and the pre-population lets its
> value="" (this is right because the value of the field in the associated
> ValidatorForm is null).
> When I submit the form, after the validation process I have value="0" (the
> field is an Integer) but I haven't set validation rules on this field :|
>
> In my opinion the validator plugin uses a default value read somewhere to
> fill the value of the hidden field (but I didn't set anything !?!)
>
> Can anyone help me?
>
> Thanks,
> aris
>
>
>
> ---------------------------------------------------------------------
> 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: Problem with validation (?)

Posted by Derek Broughton <de...@pointerstop.ca>.
On Thursday 09 December 2004 18:02, Wendy Smoak wrote:
> From: "Derek Broughton" <de...@pointerstop.ca>
>
> > > What happens when somebody types "ABCDE" into your Integer field on the
> > > form, and it fails validation?  Can you re-display the incorrect user
>
> input
>
> > > so they can see what they typed and why it was wrong?
> >
> > Or more concisely, because HTML only has string types.
>
> Oh, sure, take all the fun out of it. ;)  Teach a man to fish, and all...
>
> It begs the question though: is there a case for using Integer in an
> ActionForm?  I know the reasons *not* to (and I don't) but it must be there
> for a reason?

Beats me.   I'm still new to this.  Using strings was just something I ran 
across in a tutorial early :-)
-- 
derek

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


Re: Problem with validation (?)

Posted by Derek Broughton <de...@pointerstop.ca>.
On Thursday 09 December 2004 19:09, Laconia Data Systems wrote:
> Derek-
> Any conversions (String to Integer or whatever) need to happen in your
> Business Bean layer

Not me... I just replied to a reply. :-)
-- 
derek

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


Re: Problem with validation (?)

Posted by Hubert Rabago <hr...@gmail.com>.
One situation I might consider this acceptable is when it's for a
field that uses a drop down as the control, and I'm sure that my users
won't try to be smart with me by submitting the form using their own
hand made HTML.  In this case, I don't have to worry about the
typos/invalid values.
In my actual apps, though, I just stick with strings.

Hubert


On Thu, 09 Dec 2004 15:02:36 -0700, Wendy Smoak <ja...@wendysmoak.com> wrote:
> It begs the question though: is there a case for using Integer in an
> ActionForm?  I know the reasons *not* to (and I don't) but it must be there
> for a reason?
> 
> --
> Wendy Smoak
>

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


Re: Problem with validation (?)

Posted by Matt Bathje <mp...@ntsource.com>.
Wendy Smoak wrote:
> From: "Matt Bathje" <mp...@ntsource.com>
> 
>>What do you mean by "must be there for a reason"? As far as I know, a
>>form property can be any class type that you want it to be.
> 
> 
> Sorry, I think only in terms of dynamic forms, and there is a list of
> allowed field types:
> http://struts.apache.org/userGuide/building_controller.html#dyna_action_form_classes
> 


Ahh yes, I do remember reading that at some point. The reason I have 
forgotten about it is that I do things like:

<form-property name="thumbnail" type="org.apache.struts.upload.FormFile" />

Or even:
<form-property name="thumbnail" type="com.ntso.struts.MyVeryOwnObject[]" />

which both work fine for me under DynaValidatorForm and 
DynaValidatorActionForm (and are not considered taboo like using Integer 
afaik)



> 
> There had to be a reason that Integer made it on the list, I just wondered
> why.
> 


I have a feeling that list is a holdover from the old-timey days of 
struts, and is since outdated/invalid. Unfortunately I don't have time 
to take a look at the code and figure out what is going on for sure. 
(And possibly fix that passage...)


Matt

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


Re: Problem with validation (?)

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "Matt Bathje" <mp...@ntsource.com>

> What do you mean by "must be there for a reason"? As far as I know, a
> form property can be any class type that you want it to be.

Sorry, I think only in terms of dynamic forms, and there is a list of
allowed field types:
http://struts.apache.org/userGuide/building_controller.html#dyna_action_form_classes

> Unless I missed something, there is no special set of classes that they
> can be set to. (There is obviously a special set of classes that you
> should not set it to be as it is not best practices though...)

Prevailing wisdom is that you "should not" use anything but String/String[]
(and possibly Boolean).  Yet Integer is allowed, causing me to occasionally
snap at people who use it and then complain that it doesn't work right.

There had to be a reason that Integer made it on the list, I just wondered
why.

-- 
Wendy Smoak


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


Re: Problem with validation (?)

Posted by Matt Bathje <mp...@ntsource.com>.
Wendy Smoak wrote:
> From: "Derek Broughton" <de...@pointerstop.ca>
> 
>>>What happens when somebody types "ABCDE" into your Integer field on the
>>>form, and it fails validation?  Can you re-display the incorrect user
> 
> input
> 
>>>so they can see what they typed and why it was wrong?
>>
>>Or more concisely, because HTML only has string types.
> 
> 
> Oh, sure, take all the fun out of it. ;)  Teach a man to fish, and all...
> 
> It begs the question though: is there a case for using Integer in an
> ActionForm?  I know the reasons *not* to (and I don't) but it must be there
> for a reason?
> 

Raises not begs! (sorry, pet peeve of mine :P)

What do you mean by "must be there for a reason"? As far as I know, a 
form property can be any class type that you want it to be.

Unless I missed something, there is no special set of classes that they 
can be set to. (There is obviously a special set of classes that you 
should not set it to be as it is not best practices though...)

Matt

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


Re: Problem with validation (?)

Posted by Laconia Data Systems <we...@laconiadatasystems.com>.
Derek-
Any conversions (String to Integer or whatever) need to happen in your
Business Bean layer
e.g.
ActionForm which uses Strings and booleans
private String IntegerDisplay = null;
public String getIntegerDisplay()
{
 return this.IntegerDisplay();
}
public void setDateIntegerDisplay(String display)
{
 this.IntegerDisplay = display;
}

//Business Bean
public class BusinessBean extends BeanUtils
{
//Default constructor as per spec..
 public BusinessBean() { ; }
 public Integer intn = null;
 public String getIntegerDisplay() {
  try
 {
  intn = getInteger();
  if(ConvertUtils.isNull(intn)) return null;
  return intn.toString();
  }
 catch(NumberFormatException d)
 {
  System.out.println("Bad integer has been passed in do something");
 }
 catch(NullPointerException n)
 {
 System.out.println("Null found you passed me a null Integer!");
 }
}//end getIntegerDisplay

public void setIntegerDisplay(String display)
{
 if(display=null) intn = null;
 else
 {
  try
  { //set the value..
   setDisplay(Integer.valueOf(display));
  }
  catch(Throwable t)
  { //exception found set to null
   setDisplay(null);
  }//end catch
 } //end else
}//end setIntegerDisplay
Now what is that about teaching a man to fish?
Hope this helps,
Martin=
----- Original Message ----- 
From: "Wendy Smoak" <ja...@wendysmoak.com>
To: "Struts Users Mailing List" <us...@struts.apache.org>
Sent: Thursday, December 09, 2004 5:02 PM
Subject: Re: Problem with validation (?)


> From: "Derek Broughton" <de...@pointerstop.ca>
>
> > > What happens when somebody types "ABCDE" into your Integer field on
the
> > > form, and it fails validation?  Can you re-display the incorrect user
> input
> > > so they can see what they typed and why it was wrong?
> >
> > Or more concisely, because HTML only has string types.
>
> Oh, sure, take all the fun out of it. ;)  Teach a man to fish, and all...
>
> It begs the question though: is there a case for using Integer in an
> ActionForm?  I know the reasons *not* to (and I don't) but it must be
there
> for a reason?
>
> -- 
> Wendy Smoak
>
>
> ---------------------------------------------------------------------
> 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: Problem with validation (?)

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "Derek Broughton" <de...@pointerstop.ca>

> > What happens when somebody types "ABCDE" into your Integer field on the
> > form, and it fails validation?  Can you re-display the incorrect user
input
> > so they can see what they typed and why it was wrong?
>
> Or more concisely, because HTML only has string types.

Oh, sure, take all the fun out of it. ;)  Teach a man to fish, and all...

It begs the question though: is there a case for using Integer in an
ActionForm?  I know the reasons *not* to (and I don't) but it must be there
for a reason?

-- 
Wendy Smoak


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


Re: Problem with validation (?)

Posted by aris <ar...@agilemovement.it>.
From: "Derek Broughton" <de...@pointerstop.ca>

> On Thursday 09 December 2004 14:28, Wendy Smoak wrote:
> > From: "aris" <ar...@agilemovement.it>
> >
> > > > Use String (and occasionally Boolean) for form fields and things
will
> >
> > work
> >
> > > > the way you need them to.
> > >
> > > I don't understand your answer... If I need an Integer field for my
> > > ActionForm derived class why I've to use a String?
> >
> > What happens when somebody types "ABCDE" into your Integer field on the
> > form, and it fails validation?  Can you re-display the incorrect user
input
> > so they can see what they typed and why it was wrong?
>
> Or more concisely, because HTML only has string types.

Mmmm... I think that HTML has not types at all.


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


Re: Problem with validation (?)

Posted by Derek Broughton <de...@pointerstop.ca>.
On Thursday 09 December 2004 14:28, Wendy Smoak wrote:
> From: "aris" <ar...@agilemovement.it>
>
> > > Use String (and occasionally Boolean) for form fields and things will
>
> work
>
> > > the way you need them to.
> >
> > I don't understand your answer... If I need an Integer field for my
> > ActionForm derived class why I've to use a String?
>
> What happens when somebody types "ABCDE" into your Integer field on the
> form, and it fails validation?  Can you re-display the incorrect user input
> so they can see what they typed and why it was wrong?

Or more concisely, because HTML only has string types.
-- 
derek

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


Re: Problem with validation (?)

Posted by aris <ar...@agilemovement.it>.
From: "Wendy Smoak" <ja...@wendysmoak.com>

> From: "aris" <ar...@agilemovement.it>
>
> > > Use String (and occasionally Boolean) for form fields and things will
> work
> > > the way you need them to.
> >
> > I don't understand your answer... If I need an Integer field for my
> > ActionForm derived class why I've to use a String?
>
> What happens when somebody types "ABCDE" into your Integer field on the
> form, and it fails validation?  Can you re-display the incorrect user
input
> so they can see what they typed and why it was wrong?

Ok, so... I didn't understand your answer in the context of my question: I
had an hidden field with value="" and my question was "does ValidatorPlugin
set a default value on a field of type Integer after validation?".

By the way the answer to your second question is "no, I can't" ... but I
have an hidden field pre-populated from my Action so it's value is null or
it's an Integer....

Bye,
aris


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


Re: Problem with validation (?)

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "aris" <ar...@agilemovement.it>

> > Use String (and occasionally Boolean) for form fields and things will
work
> > the way you need them to.
>
> I don't understand your answer... If I need an Integer field for my
> ActionForm derived class why I've to use a String?

What happens when somebody types "ABCDE" into your Integer field on the
form, and it fails validation?  Can you re-display the incorrect user input
so they can see what they typed and why it was wrong?

-- 
Wendy Smoak


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


Re: Problem with validation (?)

Posted by aris <ar...@agilemovement.it>.
From: "Wendy Smoak" <ja...@wendysmoak.com>

> From: "aris" <ar...@agilemovement.it>
> > When I submit the form, after the validation process I have value="0"
(the
> > field is an Integer) but I haven't set validation rules on this field :|
>
> Use String (and occasionally Boolean) for form fields and things will work
> the way you need them to.

I don't understand your answer... If I need an Integer field for my
ActionForm derived class why I've to use a String?

> > In my opinion the validator plugin uses a default value read somewhere
to
> > fill the value of the hidden field (but I didn't set anything !?!)
>
> More likely you're seeing the result of the conversion from String to
> Integer when Struts populates your ActionForm from the request.  The best
> way to see what's actually happening is to run the whole thing in a
> debugger-- an hour with JSwat will teach you more than an entire day
> studying the documentation.

I use the debugger for my Java code but before to debug Struts Java code I
think it's a best practice to read the documentation or post a question on
the ml... don't you?

Bye,
aris.


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


Re: Problem with validation (?)

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "aris" <ar...@agilemovement.it>
> When I submit the form, after the validation process I have value="0" (the
> field is an Integer) but I haven't set validation rules on this field :|

Use String (and occasionally Boolean) for form fields and things will work
the way you need them to.

> In my opinion the validator plugin uses a default value read somewhere to
> fill the value of the hidden field (but I didn't set anything !?!)

More likely you're seeing the result of the conversion from String to
Integer when Struts populates your ActionForm from the request.  The best
way to see what's actually happening is to run the whole thing in a
debugger-- an hour with JSwat will teach you more than an entire day
studying the documentation.

-- 
Wendy Smoak



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