You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2010/11/20 01:48:30 UTC

svn commit: r1037096 - in /click/trunk/click/examples: src/org/apache/click/examples/page/form/dynamic/ webapp/form/dynamic/

Author: sabob
Date: Sat Nov 20 00:48:30 2010
New Revision: 1037096

URL: http://svn.apache.org/viewvc?rev=1037096&view=rev
Log:
convert bypass validation examples

Modified:
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java
    click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java
    click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm
    click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm
    click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm

Modified: click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java?rev=1037096&r1=1037095&r2=1037096&view=diff
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java (original)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicFieldSet.java Sat Nov 20 00:48:30 2010
@@ -64,19 +64,17 @@ public class DynamicFieldSet extends Bor
         customerFS.add(ageField);
         customerFS.add(addressChk);
 
-        // The Click script, '/click/control.js', provides the JavaScript
-        // function Click.submit(form, validate). To bypass validation
-        // specify 'false' as the second argument.
-        addressChk.setAttribute("onclick", "Click.submit(form, false)");
+        // NB: when using form.submit() the submit button cannot be
+        // called 'submit'. If it is, the browser is likely to throw a JS exception.
+        addressChk.setAttribute("onclick", "form.submit();");
 
         form.add(submit);
 
         addControl(form);
 
-        // NB: Bind and validate form fields *before* the onProcess event,
-        // enabling us to inspect Field values in the onInit event or even
-        // the Page constructor
-        boolean valid = ClickUtils.bindAndValidate(form);
+        // NB: Bind form fields *before* the onProcess event,
+        // enabling us to inspect Field values in the onInit event (or the Page constructor)
+        ClickUtils.bind(form);
 
         // We can safely check whether the user checked the addressChk
         if (addressChk.isChecked()) {
@@ -85,27 +83,26 @@ public class DynamicFieldSet extends Bor
             form.add(addressFS);
         }
 
-        form.setActionListener(new ActionListener() {
+        // NB: Bind submit. If it wasn't clicked it means the Form was submitted
+        // By JavaScript and we don't want to validate yet
+        ClickUtils.bind(submit);
+
+        // When checkbox is checked and form is submitted, we don't want to validate
+        // the partially filled form
+        if(form.isFormSubmission() && !submit.isClicked()) {
+            form.setValidate(false);
+            addModel("msg", "Validation is bypassed");
+        }
+
+        submit.setActionListener(new ActionListener() {
             private static final long serialVersionUID = 1L;
 
             public boolean onAction(Control source) {
-                return onFormSubmit();
-            }
-        });
-    }
-
-    public boolean onFormSubmit() {
-        // onFormSubmit listens on Form itself and will be invoked whenever the
-        // form is submitted.
-        if (form.isValid()) {
-
-            // Check isBypassValidation() flag whether the form validation occurred
-            if (form.isBypassValidation()) {
-                addModel("msg", "Validation bypassed");
-            } else {
+                if (form.isValid()) {
                 addModel("msg", "Form is valid after validation");
             }
-        }
-        return true;
+                return true;
+            }
+        });
     }
 }

Modified: click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java?rev=1037096&r1=1037095&r2=1037096&view=diff
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java (original)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicForm.java Sat Nov 20 00:48:30 2010
@@ -49,53 +49,47 @@ public class DynamicForm extends BorderP
     public void onInit() {
         super.onInit();
 
+        form.setJavaScriptValidation(true);
         form.add(nameField);
         form.add(checkbox);
 
-        // The Click script, '/click/control.js', provides the JavaScript
-        // function Click.submit(form, validate). To bypass validation
-        // specify 'false' as the second argument.
-        checkbox.setAttribute("onclick", "Click.submit(form, false)");
+        // NB: when using form.submit() the submit button cannot be
+        // called 'submit'. If it is, the browser is likely to throw a JS exception.
+        checkbox.setAttribute("onclick", "form.submit();");
 
         form.add(submit);
 
         addControl(form);
 
+        // NB: Bind the submit button. If it wasn't clicked it means the Form was submitted
+        // using JavaScript and we don't want to validate yet
+        ClickUtils.bind(submit);
+
+        // If submit was not clicked, don't validate
+        if(form.isFormSubmission() && !submit.isClicked()) {
+            form.setValidate(false);
+            addModel("msg", "Validation is bypassed");
+        }
+
         // NB: Bind the checkbox *before* the onProcess event, enabling us to
-        // inspect the checkbox value inside the onInit event, or even the
-        // page constructor
+        // inspect the checkbox value inside the onInit event
         ClickUtils.bind(checkbox);
 
-        // We could also bind *all* form fields in one go
-        // ClickUtils.bind(form);
-
-        // We can safely check whether the user checked the checkbox
+        // We can now check whether the user checked the checkbox
         if (checkbox.isChecked()) {
             // Dynamically add the email Field to the form
             form.add(new EmailField("email", true));
         }
 
-        form.setActionListener(new ActionListener() {
+        submit.setActionListener(new ActionListener() {
             private static final long serialVersionUID = 1L;
 
             public boolean onAction(Control source) {
-                return onFormSubmit();
-            }
-        });
-    }
-
-    public boolean onFormSubmit() {
-        // onFormSubmit listens on Form itself and will be invoked whenever the
-        // form is submitted.
-        if (form.isValid()) {
-
-            // Check isBypassValidation() flag whether the form validation occurred
-            if (form.isBypassValidation()) {
-                addModel("msg", "Validation was bypassed");
-            } else {
+                if (form.isValid()) {
                 addModel("msg", "Form is valid after validation");
             }
-        }
-        return true;
+                return true;
+            }
+        });
     }
 }

Modified: click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java?rev=1037096&r1=1037095&r2=1037096&view=diff
==============================================================================
--- click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java (original)
+++ click/trunk/click/examples/src/org/apache/click/examples/page/form/dynamic/DynamicSelect.java Sat Nov 20 00:48:30 2010
@@ -54,24 +54,16 @@ public class DynamicSelect extends Borde
 
     public DynamicSelect() {
 
-        form.setActionListener(new ActionListener() {
-            private static final long serialVersionUID = 1L;
-
-            public boolean onAction(Control source) {
-                return onFormSubmit();
-            }
-        });
-
         form.add(nameField);
 
         select.add(Option.EMPTY_OPTION);
         select.addAll(INVESTMENTS);
         form.add(select);
 
-        // The Click script, '/click/control.js', provides the JavaScript
-        // function Click.submit(form, validate). To bypass validation
-        // specify 'false' as the second argument.
-        select.setAttribute("onchange", "Click.submit(form, false)");
+        // NB: when using form.submit() the submit button cannot be
+        // called 'submit'. If it is, the browser is likely to throw a JS exception.
+        // In this demo the submit button is called 'ok'.
+        select.setAttribute("onchange", "form.submit();");
 
         form.add(submit);
 
@@ -88,22 +80,23 @@ public class DynamicSelect extends Borde
             address.add(new DoubleField("amount", true));
             form.add(address);
         }
-    }
 
-    // Event Handlers ---------------------------------------------------------
+        // When checkbox is checked and form is submitted, we don't want to validate
+        // the partially filled in form
+        if(form.isFormSubmission() && !submit.isClicked()) {
+            form.setValidate(false);
+            addModel("msg", "Validation is bypassed");
+        }
+
+        submit.setActionListener(new ActionListener() {
+            private static final long serialVersionUID = 1L;
 
-    public boolean onFormSubmit() {
-        // onFormSubmit listens on Form itself and will be invoked whenever the
-        // form is submitted.
-        if (form.isValid()) {
-
-            // Check isBypassValidation() flag whether the form validation occurred
-            if (form.isBypassValidation()) {
-                addModel("msg", "Validation bypassed");
-            } else {
+            public boolean onAction(Control source) {
+                if (form.isValid()) {
                 addModel("msg", "Form is valid after validation");
             }
-        }
-        return true;
+                return true;
+            }
+        });
     }
 }

Modified: click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm?rev=1037096&r1=1037095&r2=1037096&view=diff
==============================================================================
--- click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm (original)
+++ click/trunk/click/examples/webapp/form/dynamic/dynamic-field-set.htm Sat Nov 20 00:48:30 2010
@@ -1,12 +1,13 @@
 This example demonstrates how to dynamically add fieldsets to a form.
 <ul>
     <li>
-        The JavaScript function <tt>Click.submit(form, validate)</tt> is set as
-        the checkbox <tt>onclick</tt> handler. Clicking the checkbox will submit
-        the form, but bypass form validation.
+        The form is submitted using the JavaScript statement, <tt>form.submit()</tt>,
+        which is set as the Checkbox <tt>onclick</tt> handler. Clicking the checkbox
+        will submit the form. On the server, Form
+        <tt>validation</tt> is switched off through, <tt>Form.setValidate(false)</tt>.
     </li>
     <li>
-        The utility method <tt>ClickUtils.bindAndValidate(Form)</tt> is used
+        The utility method <tt>ClickUtils.bind(Form)</tt> is used
         to bind and inspect the checkbox value <tt>before</tt> the page onProcess
         event.
     </li>

Modified: click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm?rev=1037096&r1=1037095&r2=1037096&view=diff
==============================================================================
--- click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm (original)
+++ click/trunk/click/examples/webapp/form/dynamic/dynamic-form.htm Sat Nov 20 00:48:30 2010
@@ -1,9 +1,10 @@
 This example demonstrates how to dynamically add fields to a form.
 <ul>
     <li>
-        The JavaScript function <tt>Click.submit(form, validate)</tt> is set as
-        the checkbox <tt>onclick</tt> handler. Clicking the checkbox will submit
-        the form, but bypass form validation.
+        The form is submitted using the JavaScript statement, <tt>form.submit()</tt>,
+        which is set as the Checkbox <tt>onclick</tt> handler. Clicking the checkbox
+        will submit the form. On the server, Form
+        <tt>validation</tt> is switched off through, <tt>Form.setValidate(false)</tt>.
     </li>
     <li>
         The utility method <tt>ClickUtils.bind(Control)</tt> is used to bind

Modified: click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm?rev=1037096&r1=1037095&r2=1037096&view=diff
==============================================================================
--- click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm (original)
+++ click/trunk/click/examples/webapp/form/dynamic/dynamic-select.htm Sat Nov 20 00:48:30 2010
@@ -1,17 +1,19 @@
-This example demonstrates how to dynamically add fieldsets to a form.
+This example demonstrates how to dynamically add fields to a form through a
+select control.
 <ul>
     <li>
-        The JavaScript function <tt>Click.submit(form, validate)</tt> is set as
-        the Select <tt>onchange</tt> handler. Changing the select option will
-        submit the form, but bypass form validation.
+        The form is submitted using the JavaScript statement, <tt>form.submit()</tt>,
+        which is set as the Select <tt>onchange</tt> handler. Changing the select
+        option will submit the form. On the server, Form
+        <tt>validation</tt> is switched off through, <tt>Form.setValidate(false)</tt>.
     </li>
     <li>
         The utility method <tt>ClickUtils.bind(Form)</tt> is used to bind
         and inspect the Select value <tt>before</tt> the page onProcess event.
     </li>
     <li>
-        If the selected value is "Property" an address fieldset is added to the form.
-        If the selected value is "Stocks" an amount field is added to the form.
+        If the selected value is "Property", an address fieldset is added to the form.
+        If the selected value is "Stocks", an amount field is added to the form.
     </li>
 
 </ul>