Here is my patch for form processing/refactored button. Its not polished
yet, but if it looks good to you guys I can polish it up.

Igor
 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Eelco
Hillenius
Sent: Tuesday, July 05, 2005 9:20 AM
To: [email protected]
Subject: Re: [Wicket-develop] [ wicket-Bugs-1220639 ] form processing could
be better defined

Yeah, send in your patches please. I had some ideas myself, so lets see what
our combined effort results in. Can apply tonight.

Eelco

Phil Kulak wrote:

>haha, that's almost exactly what I was thinking. I actually added a 
>method to button called "validatesForm". It seems to work well with 
>this onValidate() method:
>
>protected void onValidate()
>{
>       final Button button = findSubmittingButton();
>       if (button == null)
>       {
>               validate();
>       }
>       else
>       {
>               if (button.validatesForm())
>               {
>                       if (validate())
>                       {
>                               button.onSubmit();
>                       }
>               }
>               else
>               {
>                       button.onSubmit();
>               }
>       }
>}
>
>On 7/5/05, Igor Vaynberg <[EMAIL PROTECTED]> wrote:
>  
>
>>When do you guys think this will be semi-finished in HEAD?
>>
>>Here is the functionality im looking for:
>>I would like to be able to specify whether or not the form should 
>>validate based on the submitting button and to have the button's 
>>onsubmit handler called before the forms onsubmit handler.
>>
>>For example:
>>Button can have a boolean validateForm() and a boolean onSubmit(), if 
>>false is returned from the button's onSubmit() the form's onSubmit() 
>>doesn't get called.
>>
>>My usecase is a wizard. When the user presses the back button in the 
>>wizard I would like to record the user's input and go back a step w/o 
>>validation and onsubmit() being called.
>>
>>If this is on par with what you guys had in mind I would be happy to 
>>submit a patch.
>>
>>Thanks,
>>Igor
>>
>>    
>>



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies from
IBM. Find simple to follow Roadmaps, straightforward articles, informative
Webcasts and more! Get everything you need to get up to speed, fast.
http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click
_______________________________________________
Wicket-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-develop


Index: src/java/wicket/markup/html/form/Button.java
===================================================================
RCS file: /cvsroot/wicket/wicket/src/java/wicket/markup/html/form/Button.java,v
retrieving revision 1.10
diff -u -r1.10 Button.java
--- src/java/wicket/markup/html/form/Button.java        25 Mar 2005 15:44:43 
-0000      1.10
+++ src/java/wicket/markup/html/form/Button.java        5 Jul 2005 19:18:44 
-0000
@@ -19,7 +19,6 @@
 
 import wicket.WicketRuntimeException;
 import wicket.markup.ComponentTag;
-import wicket.model.IModel;
 import wicket.util.string.Strings;
 import wicket.util.value.ValueMap;
 
@@ -28,7 +27,7 @@
  * 
  * @author Jonathan Locke
  */
-public class Button extends FormComponent
+public class Button extends AbstractButton
 {
        /**
         * @see wicket.Component#Component(String)
@@ -39,24 +38,6 @@
        }
 
        /**
-        * @return Any onClick JavaScript that should be used
-        */
-       protected String getOnClickScript()
-       {
-               return null;
-       }
-
-       /**
-        * @see wicket.Component#initModel()
-        */
-       protected IModel initModel()
-       {
-               // Buttons don't have models and so we don't want
-               // Component.initModel() to try to attach one automatically.
-               return null;
-       }
-
-       /**
         * Processes the component tag.
         * 
         * @param tag
@@ -87,30 +68,5 @@
                {
                        throw new WicketRuntimeException("Button tag must have 
non-empty value attribute");
                }
-
-               // Default handling for component tag
-               super.onComponentTag(tag);
-
-               // If the subclass specified javascript, use that
-               final String onClickJavaScript = getOnClickScript();
-               if (onClickJavaScript != null)
-               {
-                       tag.put("onclick", onClickJavaScript);
-               }
-       }
-
-       /**
-        * Override this method to provide special submit handling in a 
multi-button
-        * form
-        */
-       protected void onSubmit()
-       {
-       }
-
-       /**
-        * @see wicket.markup.html.form.FormComponent#updateModel()
-        */
-       protected void updateModel()
-       {
        }
 }
\ No newline at end of file
Index: src/java/wicket/markup/html/form/Form.java
===================================================================
RCS file: /cvsroot/wicket/wicket/src/java/wicket/markup/html/form/Form.java,v
retrieving revision 1.80
diff -u -r1.80 Form.java
--- src/java/wicket/markup/html/form/Form.java  30 Jun 2005 14:39:46 -0000      
1.80
+++ src/java/wicket/markup/html/form/Form.java  5 Jul 2005 19:18:44 -0000
@@ -312,15 +312,36 @@
         * 
         * Handles form submissions. By default, this method simply calls 
validate()
         * to validate the form and update the model if there is only one 
button. If
-        * there is more than one button, it calls the onClick() method for the
+        * there is more than one button, it calls the onSubmit() method for the
         * button which submitted the form.
         * 
         * @see Form#validate()
         */
        public void onFormSubmitted()
        {
-               // Validate form
-               onValidate();
+               if (countButtons()>1) {
+                       final AbstractButton button=findSubmittingButton();
+                       if (button == null)
+                       {
+                               throw new WicketRuntimeException("Unable to 
find submitting button");
+                       } 
+                       
+                       boolean callOnSubmit=true;
+                       if (button.validatesForm()) {
+                               callOnSubmit=validate();
+                       }
+                       if (callOnSubmit) {
+                               if (button.onSubmit()) {
+                                       onSubmit();
+                               }
+                       }
+               } else {
+                       if (validate()) {
+                               onSubmit();
+                       }
+               }
+               
+               
        }
 
        /**
@@ -429,31 +450,6 @@
        }
 
        /**
-        * Called when a form that has been submitted needs to be validated.
-        */
-       protected void onValidate()
-       {
-               // Validate the form
-               if (validate())
-               {
-                       // If there is more than one button, we also call the 
Button's
-                       // onSubmit() handler
-                       if (countButtons() > 1)
-                       {
-                               final Button button = findSubmittingButton();
-                               if (button == null)
-                               {
-                                       throw new 
WicketRuntimeException("Unable to find submitting button");
-                               }
-                               else
-                               {
-                                       button.onSubmit();
-                               }
-                       }
-               }
-       }
-
-       /**
         * Validates the form and updates the models of form components. If the 
form
         * validates successfully, handleValidSubmit() is called. If not,
         * handleErrors() is called.
@@ -488,9 +484,6 @@
                        // Persist FormComponents if requested
                        persistFormComponentData();
 
-                       // Model was successfully updated with valid data
-                       onSubmit();
-                       
                        // Form validated
                        return true;
                }
@@ -535,7 +528,7 @@
        private int countButtons()
        {
                final Count count = new Count();
-               visitChildren(Button.class, new IVisitor()
+               visitChildren(AbstractButton.class, new IVisitor()
                {
                        public Object component(final Component component)
                        {
@@ -549,14 +542,14 @@
        /**
         * @return The button which submitted this form
         */
-       private Button findSubmittingButton()
+       private AbstractButton findSubmittingButton()
        {
-               return (Button)visitChildren(Button.class, new IVisitor()
+               return (AbstractButton)visitChildren(AbstractButton.class, new 
IVisitor()
                {
                        public Object component(final Component component)
                        {
                                // Get button
-                               final Button button = (Button)component;
+                               final AbstractButton button = 
(AbstractButton)component;
 
                                // Check for button-name or button-name.x 
request string
                                if (!Strings.isEmpty(button.getInput())
Index: src/java/wicket/markup/html/form/AbstractButton.java
===================================================================
RCS file: src/java/wicket/markup/html/form/AbstractButton.java
diff -N src/java/wicket/markup/html/form/AbstractButton.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ src/java/wicket/markup/html/form/AbstractButton.java        1 Jan 1970 
00:00:00 -0000
@@ -0,0 +1,83 @@
+package wicket.markup.html.form;
+
+import wicket.markup.ComponentTag;
+import wicket.model.IModel;
+
+/**
+ * Base class for controls that submit a form.
+ * 
+ * @author Igor Vaynberg [EMAIL PROTECTED]
+ *
+ */
+public abstract class AbstractButton extends FormComponent
+{
+       /**
+        * @see wicket.Component#Component(String)
+        */
+       public AbstractButton(String id)
+       {
+               super(id);
+       }
+
+       protected boolean validatesForm() {
+               return true;
+       }
+       
+       /**
+        * @return Any onClick JavaScript that should be used
+        */
+       protected String getOnClickScript()
+       {
+               return null;
+       }
+
+       /**
+        * @see wicket.Component#initModel()
+        */
+       protected IModel initModel()
+       {
+               // Buttons don't have models and so we don't want
+               // Component.initModel() to try to attach one automatically.
+               return null;
+       }
+
+       /**
+        * Override this method to provide special submit handling in a 
multi-button
+        * form.
+        * @return true to call form.onSubmit(), false otherwise
+        */
+       protected boolean onSubmit()
+       {
+               return true;
+       }
+
+       /**
+        * @see wicket.markup.html.form.FormComponent#updateModel()
+        */
+       protected void updateModel()
+       {
+       }
+       
+
+       /**
+        * Processes the component tag.
+        * 
+        * @param tag
+        *            Tag to modify
+        * @see wicket.Component#onComponentTag(ComponentTag)
+        */
+       protected void onComponentTag(final ComponentTag tag)
+       {
+               // Default handling for component tag
+               super.onComponentTag(tag);
+
+               // If the subclass specified javascript, use that
+               final String onClickJavaScript = getOnClickScript();
+               if (onClickJavaScript != null)
+               {
+                       tag.put("onclick", onClickJavaScript);
+               }
+       }
+
+
+}

Reply via email to