You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by cool dude <co...@yahoo.com> on 2002/02/09 13:29:46 UTC

Form Bean Validation problem

Hi,
   Here's a small problem that I'm facing. I have a
form which has an integer field, say a cost of some
item. Now, I need to do the validation for this
form-field to make sure that the user does not enter a
non-numeric data in this field. I have the form
containing this field as a primitive int. The problem
is that whenever a non-numberic value is entered the
value is reset to zero. This leads to two problems,
one it doesn't give me a chance to find out that the
user has entered a non-numeric value in my validate
method & secondly, I end up losing the value that the
user had entered & hence can't show him the value
back. The rather crude solution to this is to make the
numeric form-field value a String & do the check in
the validate() to find if a non-numeric value is
entered. But I'm not happy with this solution as it
requires me to decalre a field as string even when it
should have been an int or float ...

Is there any other way around this prob?

Thanx in advance,
VD.

__________________________________________________
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Form Bean Validation problem

Posted by Pavel Nejedly <bi...@atrey.karlin.mff.cuni.cz>.
On Sat, Feb 09, 2002 at 04:29:46AM -0800, cool dude wrote:
# Hi,
#    Here's a small problem that I'm facing. I have a
# form which has an integer field, say a cost of some
# item. Now, I need to do the validation for this
# form-field to make sure that the user does not enter a
# non-numeric data in this field. I have the form
# containing this field as a primitive int. The problem
# is that whenever a non-numberic value is entered the
# value is reset to zero. This leads to two problems,
# one it doesn't give me a chance to find out that the
# user has entered a non-numeric value in my validate
# method & secondly, I end up losing the value that the
# user had entered & hence can't show him the value
# back. The rather crude solution to this is to make the
# numeric form-field value a String & do the check in
# the validate() to find if a non-numeric value is
# entered. But I'm not happy with this solution as it
# requires me to decalre a field as string even when it
# should have been an int or float ...

  I use two variables in the bean for the same non-String
  property, like this:

  class myForm extends ActionForm {

    private String price;
    private float priceAsFloat;

    public String getPrice () { return this.price; } 
      /* this is called by <html:text> */
    public void setPrice (String price) { this.price = price; }
      /* this is called during submit */

    public float getPriceAsFloat() { return this.priceAsFloat; }
      /* this is used in Action */
    /* note no settter method for priceAsFloat */

    public ActionErrors validate (...) {

      ActionErrors errors = new ActionErrors ();

      /* validate () is the perfect place to convert String
       * data to int, Date or whatever else
       */
      
      try {
        priceAsFloat = Float.parseFloat (price);
      } catch (Exception e) {
        errors.add ("price", new ActionError("invalid.price"));
      }

      return errors;
    }
  }

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Form Bean Validation problem

Posted by Ted Husted <hu...@apache.org>.
As Robert mentioned, the best practice is to declare all the ActionForm
properties as Strings or booleans. 

The purpose of the ActionForm is not to represent your business data. It
is to serve as a buffer for the HTML controls. HTTP is String-based, and
so the ActionForm properties need to be String-based too. Otherwise, as
you have discovered, invalid values disappear, and cannot be presented
to the user for correction. 

Once the String values have been validated, you can then transform them
to their proper type, and store them in a business bean designed for
this purpose. 

Another important function of the ActionForm is to serve as a firewall
between the Web users and the rest of your application. We have no
control over what insane values someone can toss at our actions via
HTTP. The ActionForm is a DMZ where we can accept whatever value is
tendered, but validate it before it passed along to business objects
(which tend to be more trusting). 

For more about the design of ActionForms, see also

http://www.mail-archive.com/struts-user@jakarta.apache.org/msg19338.html

http://www.mail-archive.com/struts-user@jakarta.apache.org/msg08070.html

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Java Web Development with Struts.
-- Tel +1 585 737-3463.
-- Web http://www.husted.com/struts/



cool dude wrote:
> 
> Hi,
>    Here's a small problem that I'm facing. I have a
> form which has an integer field, say a cost of some
> item. Now, I need to do the validation for this
> form-field to make sure that the user does not enter a
> non-numeric data in this field. I have the form
> containing this field as a primitive int. The problem
> is that whenever a non-numberic value is entered the
> value is reset to zero. This leads to two problems,
> one it doesn't give me a chance to find out that the
> user has entered a non-numeric value in my validate
> method & secondly, I end up losing the value that the
> user had entered & hence can't show him the value
> back. The rather crude solution to this is to make the
> numeric form-field value a String & do the check in
> the validate() to find if a non-numeric value is
> entered. But I'm not happy with this solution as it
> requires me to decalre a field as string even when it
> should have been an int or float ...
> 
> Is there any other way around this prob?
> 
> Thanx in advance,
> VD.
> 
> __________________________________________________
> Do You Yahoo!?
> Send FREE Valentine eCards with Yahoo! Greetings!
> http://greetings.yahoo.com
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Form Bean Validation problem

Posted by Robert Taylor <rt...@mulework.com>.
Albeit crude, I think that declaring it as a String is the right thing to
do. The value is sent across the wire as a string and Struts attempts to
convert it to the proper data type using introspection. The advantage of
declaring it as a String is that you have more granular control over
validation as you have already mentioned. Once your form has properly
validated the field, then you can safely use Integer.parseInt() in the
Action class to convert it to the expected primitive value.

My forms are usually contain all String fields. I then use an exportObject()
which returns a value object with the data in the appropriate
native format (ints, floats, Date, etc...) I can then pass the value object
to a business delegate to perform any business logic processing.
I also have an importObject() which maps and converts the value object data
members into the appropriate String fields.

That's just my preference.

robert

> -----Original Message-----
> From: cool dude [mailto:cool_dude_22_in@yahoo.com]
> Sent: Saturday, February 09, 2002 7:30 AM
> To: struts-user@jakarta.apache.org
> Subject: Form Bean Validation problem
>
>
> Hi,
>    Here's a small problem that I'm facing. I have a
> form which has an integer field, say a cost of some
> item. Now, I need to do the validation for this
> form-field to make sure that the user does not enter a
> non-numeric data in this field. I have the form
> containing this field as a primitive int. The problem
> is that whenever a non-numberic value is entered the
> value is reset to zero. This leads to two problems,
> one it doesn't give me a chance to find out that the
> user has entered a non-numeric value in my validate
> method & secondly, I end up losing the value that the
> user had entered & hence can't show him the value
> back. The rather crude solution to this is to make the
> numeric form-field value a String & do the check in
> the validate() to find if a non-numeric value is
> entered. But I'm not happy with this solution as it
> requires me to decalre a field as string even when it
> should have been an int or float ...
>
> Is there any other way around this prob?
>
> Thanx in advance,
> VD.
>
> __________________________________________________
> Do You Yahoo!?
> Send FREE Valentine eCards with Yahoo! Greetings!
> http://greetings.yahoo.com
>
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>