Martin Cooper wrote:

> My understanding is that this is how it's supposed to work. The idea behind
> form beans is that they represent exactly what the user entered, so that the
> same information can be used to populate the form when errors occur. A
> struts:text value is, not surprisingly, text, so transforming it to an
> Integer does not retain all of the information required to do this. For
> example, you cannot distinguish between whether the user entered "0" or "".
>

I really don't agree with that. To me, a java.lang.Integer object should be kept
to null and not initialized to 0 if the user didn't enter anything. This seems
much more logical to me. BTW, this is how it works in the database/EJB world. If
you read an integer from a database, you get back an object of type
java.lang.Integer, and it's set to null if the int in the database was null.

JB.

>
> The right way to do this is, I believe, to have all your form bean fields be
> Strings, and do any necessary type conversions as the Action needs to access
> the values in the form bean. The form bean can (and probably should) do the
> validation, though, to ensure that the type conversions will succeed at that
> time.
>
> --
> Martin Cooper
> Tumbleweed Communications
>
> ----- Original Message -----
> From: "Matthias Kerkhoff" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, October 23, 2000 2:23 AM
> Subject: Problem with optional form fields
>
> > Hi all,
> >
> > I've noticed a problem with forms containing optional fields.
> >
> > Suppose you have a form containing some optional fields, that the
> > user can fill with some value or leave blank. If form validation
> > fails (for example because the user has not filled in values for
> > some _other_ mandatory fields) the usual pattern is to redisplay
> > the form page to the user together with some error messages.
> >
> > However, due to the way the form-field tags like <struts:text>
> > are implemented, the optional fields will then contain default
> > values, if the associated properties are not of type String.
> >
> >
> > Example:
> >
> > Imagine a slightly modified subscription.jsp from the struts-example,
> > that asks the user for an additional (and optional) Integer property.
> >
> >   <tr>
> >     <th align="right">
> >       <struts:message key="prompt.intProperty"/>
> >     </th>
> >     <td align="left">
> >       <struts:text property="intProperty"/>
> >     </td>
> >   </tr>
> >
> > If the user enters invalid values for - let's say - the mail hostname
> > and leaves the intProperty field blank, the subscription.jsp will be
> > redisplayed. This time, the intProperty field will contain a 0 (instead
> > of being empty). Depending on the semantic behind intProperty this could
> > make a serious difference, when this form is resubmitted.
> >
> > Is this behaviour intended ?
> >
> > --
> > Matthias                          mailto:[EMAIL PROTECTED]
> >
> >

--
Jean-Baptiste Nizet
[EMAIL PROTECTED]

R&D Engineer, S1 Belgium
Kleine Kloosterstraat, 23
B-1932 Sint-Stevens Woluwe
+32 2 200 45 42


Reply via email to