Thanks. A couple of remarks:

1. Why have an AbstractButton? Button with less strict tag testing would be fine too right? 2. I'd prefer not to have onSubmit return a value, as in that case submit does two different things. I think it is a good idea to be able to have different validations for different buttons though. Why not a validate method on Button? And what about IFormValidator? That was meant for validion on form level (like when you compare more fields with each other), but we might use the same principle here. a. If we would implement the IFormValidator thing in Button, there is no need to be able to do validation otherwise, right? I understand that having e.g. a simple method 'boolean validate' is convenient, but: - it doesn't 'force' people to set feedback messages (which they should do as otherwise the failure is pretty useless); - IFormValidator is to be prefered as it is much easier to reuse validations with that. Sure, you could implement your own delegate mechanism, but that wouldn't be obvious. 3. One of the things I wanted to tackle with this issue was that code like updating form components in a validate method doesn't make much sense. The whole processing should be broken up in clearer steps with methods that fit them, I'd like to see whether IFormValidationStrategy still suffices and we may need to provide a couple of call back/ template methods so that users can 'intercept' in the form processing. I'm still thinking about the best way, but jet lag strikes again.

Eelco


Igor Vaynberg wrote:

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);
+               }
+       }
+
+
+}



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

Reply via email to