You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by James Turner <tu...@blackbear.com> on 2002/05/12 07:34:55 UTC

[PATCH] Allow boolean fields to be cleared correctly on form submit

Humbly submitted for your approval:

Wanting to be able to have checkboxes automatically be set correctly by 
form submissions, I propose to add a method to ActionForm called 
getBooleanProperties, which will return an array of strings, which 
correspond to properties that are boolean in nature.  RequestUtils.populate 
uses this list to clear all the properties to false before processing the 
request parameters, ensuring that if a checkbox is set, and then cleared on 
a resubmit, the bean property is appropriately updated to false.

And yes, I've coded around this by examining the request in the validate 
method and hand-setting the properties if they aren't found in the request, 
but it limits the utility of validate then, since I can't use it on final 
submission (I use 4 different forms in a larger transaction) to validate 
each form one last time.

Whaddya think?
James

Index: ActionForm.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/action/ActionForm.java,v
retrieving revision 1.10
diff -u -r1.10 ActionForm.java
--- ActionForm.java	21 Nov 2001 13:59:28 -0000	1.10
+++ ActionForm.java	12 May 2002 05:24:42 -0000
@@ -120,6 +120,16 @@


      /**
+     * Define any form fields properties associated with booleans such as 
checkboxes, which
+     * will not be cleared by normal HTTP request processing.  These 
properties will be set to
+     * false manually by RequestUtils.populate
+     */
+
+    public String[] getBooleanProperties() {
+	return null;
+    }
+
+    /**
       * Return the controller servlet instance to which we are attached.
       */
      protected ActionServlet getServlet() {
Index: RequestUtils.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v
retrieving revision 1.35
diff -u -r1.35 RequestUtils.java
--- RequestUtils.java	8 Apr 2002 04:30:41 -0000	1.35
+++ RequestUtils.java	12 May 2002 05:25:06 -0000
@@ -934,6 +934,20 @@
              request.removeAttribute(Action.MAPPING_KEY);
          }

+	/* Set any boolean fields to false before iterating over properties */
+	String[] booleanProperties = ((ActionForm)bean).getBooleanProperties();
+	if (booleanProperties != null) {
+	    for (int i = 0; i < booleanProperties.length; i++) {
+		properties.put(booleanProperties[i], "false");
+	    }
+	    try {
+		BeanUtils.populate(bean, properties);
+	    } catch (Exception e) {
+		throw new ServletException("BeanUtils.populate", e);
+	    }
+	    properties.clear();
+	}
+
          if (!isMultipart) {
              names = request.getParameterNames();
          }


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


Re: [PATCH] Allow boolean fields to be cleared correctly on form submit

Posted by Erik Hatcher <ja...@ehatchersolutions.com>.
Why not just do this in your ActionForm subclass reset method?  This is the
traditional way to accomplish this feat.  And you really wanted (not sure
why you'd want to though), you could have an abstract subclass of ActionForm
that you always subclass which overrides reset and does something similar to
what you are proposing below.... but then you'd have to deal with a subclass
overriding reset itself somehow.

Anyway, my recommendation: use reset - thats what its for!  :)

    Erik


----- Original Message -----
From: "James Turner" <tu...@blackbear.com>
To: <st...@jakarta.apache.org>
Sent: Sunday, May 12, 2002 1:34 AM
Subject: [PATCH] Allow boolean fields to be cleared correctly on form submit


> Humbly submitted for your approval:
>
> Wanting to be able to have checkboxes automatically be set correctly by
> form submissions, I propose to add a method to ActionForm called
> getBooleanProperties, which will return an array of strings, which
> correspond to properties that are boolean in nature.
RequestUtils.populate
> uses this list to clear all the properties to false before processing the
> request parameters, ensuring that if a checkbox is set, and then cleared
on
> a resubmit, the bean property is appropriately updated to false.
>
> And yes, I've coded around this by examining the request in the validate
> method and hand-setting the properties if they aren't found in the
request,
> but it limits the utility of validate then, since I can't use it on final
> submission (I use 4 different forms in a larger transaction) to validate
> each form one last time.
>
> Whaddya think?
> James
>
> Index: ActionForm.java
> ===================================================================
> RCS file:
>
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/action/ActionForm
.java,v
> retrieving revision 1.10
> diff -u -r1.10 ActionForm.java
> --- ActionForm.java 21 Nov 2001 13:59:28 -0000 1.10
> +++ ActionForm.java 12 May 2002 05:24:42 -0000
> @@ -120,6 +120,16 @@
>
>
>       /**
> +     * Define any form fields properties associated with booleans such as
> checkboxes, which
> +     * will not be cleared by normal HTTP request processing.  These
> properties will be set to
> +     * false manually by RequestUtils.populate
> +     */
> +
> +    public String[] getBooleanProperties() {
> + return null;
> +    }
> +
> +    /**
>        * Return the controller servlet instance to which we are attached.
>        */
>       protected ActionServlet getServlet() {
> Index: RequestUtils.java
> ===================================================================
> RCS file:
>
/home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/RequestUtils
.java,v
> retrieving revision 1.35
> diff -u -r1.35 RequestUtils.java
> --- RequestUtils.java 8 Apr 2002 04:30:41 -0000 1.35
> +++ RequestUtils.java 12 May 2002 05:25:06 -0000
> @@ -934,6 +934,20 @@
>               request.removeAttribute(Action.MAPPING_KEY);
>           }
>
> + /* Set any boolean fields to false before iterating over properties */
> + String[] booleanProperties = ((ActionForm)bean).getBooleanProperties();
> + if (booleanProperties != null) {
> +     for (int i = 0; i < booleanProperties.length; i++) {
> + properties.put(booleanProperties[i], "false");
> +     }
> +     try {
> + BeanUtils.populate(bean, properties);
> +     } catch (Exception e) {
> + throw new ServletException("BeanUtils.populate", e);
> +     }
> +     properties.clear();
> + }
> +
>           if (!isMultipart) {
>               names = request.getParameterNames();
>           }
>
>
> --
> 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>