Matthias Kerkhoff wrote:

> 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 ?
>

It is a side effect of what is really happening.

The browser has no real clue that the field is an integer field -- it just
thinks there is text.  Therefore, what it sends as input for an empty input
field is a zero-length string, which (unfortunately) the BeanUtils populate
method treats as a zero when setting an integer field.

It might make sense to skip calling the setIntProperty() field at all if the
input is a zero-length string.  JSP follows this rule when you specify
something like <jsp:setProperty name="beanname" property="*"/>, so this
would be more consistent with JSP as well.

What do you think?

> Matthias                          mailto:[EMAIL PROTECTED]

Craig McClanahan

====================
See you at ApacheCon Europe <http://www.apachecon.com>!
Session VS01 (23-Oct 13h00-17h00):  Sun Technical Briefing
Session T06  (24-Oct 14h00-15h00):  Migrating Apache JServ
                                    Applications to Tomcat


Reply via email to