click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r797964 - in /incubator/click/trunk/click: documentation/docs/roadmap-changes.html framework/src/org/apache/click/control/Field.java framework/src/org/apache/click/control/Form.java
Date Sun, 26 Jul 2009 17:52:02 GMT
Author: sabob
Date: Sun Jul 26 17:52:01 2009
New Revision: 797964

URL: http://svn.apache.org/viewvc?rev=797964&view=rev
Log:
improved Form so that subclasses can have cross-field validation. CLK-572

Modified:
    incubator/click/trunk/click/documentation/docs/roadmap-changes.html
    incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java
    incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java

Modified: incubator/click/trunk/click/documentation/docs/roadmap-changes.html
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/documentation/docs/roadmap-changes.html?rev=797964&r1=797963&r2=797964&view=diff
==============================================================================
--- incubator/click/trunk/click/documentation/docs/roadmap-changes.html (original)
+++ incubator/click/trunk/click/documentation/docs/roadmap-changes.html Sun Jul 26 17:52:01
2009
@@ -78,8 +78,9 @@
 <h2>Changes</h2>
 
 <dl>
-  <dt><a name="2.1.0-RC2"></a>
-  <div class="release-header">Release Candidate - Version 2.1.0 RC2 - ??? July 2009</div></dt>
+  <dt><a name="2.1.0"></a>
+    <div class="release-header">Latest Stable Release - Version 2.1.0 - ??? August
2009</div>
+  </dt>
   <dd>
     <div style="margin-left:-2em;margin-bottom:1em;">
       IMPORTANT be sure to read the <a href="upgrade-path.html">upgrade path</a>
@@ -184,7 +185,7 @@
           that can be used for uploading files in a <a class="external" target="_blank"
href="http://code.google.com/appengine/docs/java/overview.html">Google App Engine</a>
           environment.
       </li>
-       <li class="change">
+      <li class="change">
           Added support for templates with custom extensions through the new
           ConfigService.<a href="click-api/org/apache/click/service/ConfigService.html#isTemplate(java.lang.String)">isTemplate</a>
           method. The default ConfigService implementation, XmlConfigService, provides
@@ -278,6 +279,16 @@
           [<a target='_blank' href="https://issues.apache.org/jira/browse/CLK-556">556</a>].
       </li>
       <li class="change">
+          Improved Form validation to allow Form subclasses to override the
+          <a href="click-api/org/apache/click/control/Form.html#validate()">validate</a>
+          method and implement cross-field validation. The following
+          changes were made: the previous <tt>validate</tt> method was renamed
+          to <a href="click-api/org/apache/click/control/Form.html#validateFileUpload()">validateFileUpload</a>
+          and a new empty <a href="click-api/org/apache/click/control/Form.html#validate()">validate</a>
+          method was introduced, that can safely be overridden in subclasses
+          [<a target='_blank' href="https://issues.apache.org/jira/browse/CLK-572">572</a>].
+      </li>
+      <li class="change">
           Improved Page redirect to support parameters. See the new Page methods
           <a href="click-api/org/apache/click/Page.html#setRedirect(java.lang.String,
java.util.Map)">setRedirect(String, Map)</a>
           and <a href="click-api/org/apache/click/Page.html#setRedirect(java.lang.Class,
java.util.Map)">setRedirect(Class, Map)</a>

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java?rev=797964&r1=797963&r2=797964&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/Field.java Sun Jul
26 17:52:01 2009
@@ -909,7 +909,7 @@
      * the request submission. Field subclasses should override this method
      * to implement request validation logic.
      * <p/>
-     * If the field determines that the submission is invalid it should set
+     * If the field determines that the submission is invalid it should set the
      * {@link #error} property with the error message.
      */
     public void validate() {

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java?rev=797964&r1=797963&r2=797964&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java Sun Jul 26
17:52:01 2009
@@ -172,11 +172,14 @@
  * request is made the form will validate the field values. To disable
  * automatic validation set {@link #setValidate(boolean)} to false.
  * <p/>
+ * Form also provides a {@link #validate()} method where subclasses can provide
+ * custom cross-field validation.
+ * <p/>
  * <b>File Upload Validation</b>
  * <p/>
- * Form {@link #validate()} method provides special validation for multipart requests
- * (multipart requests are used for when files are uploaded from the browser).
- * The {@link #validate()} method checks that files being uploaded do not exceed the
+ * The Form's {@link #validateFileUpload()} provides validation for multipart
+ * requests (multipart requests are used for uploading files from the browser).
+ * The {@link #validateFileUpload()} method checks that files being uploaded do not exceed
the
  * {@link org.apache.click.service.CommonsFileUploadService#sizeMax maximum request size}
  * or the {@link org.apache.click.service.CommonsFileUploadService#fileSizeMax maximum file
size}.
  * <p/>
@@ -184,7 +187,7 @@
  * is exceeded, the request is deemed invalid ({@link #hasPostError hasPostError}
  * will return true), and no further processing is performed on the form or fields.
  * Instead the form will display the appropriate error message for the invalid request.
- * See {@link #validate()} for details of the error message properties.
+ * See {@link #validateFileUpload()} for details of the error message properties.
  * <p/>
  * <b>JavaScript Validation</b>
  * <p/>
@@ -1731,6 +1734,9 @@
      * <li>Invoke the Forms listener if defined</li>
      * </ol>
      *
+     * This method delegates validation to {@link #validate()} while
+     * file upload validation are delegated to {@link #validateFileUpload()}.
+     *
      * @see org.apache.click.Context#getRequestParameter(String)
      * @see org.apache.click.Context#getFileItemMap()
      *
@@ -1738,18 +1744,16 @@
      */
     public boolean onProcess() {
 
-        if (getValidate()) {
-            validate();
+        validateFileUpload();
 
-            // If a POST error occurred exit early.
-            if (hasPostError()) {
-                // Remove exception to ensure other forms on Page do not
-                // validate twice for same error.
-                getContext().getRequest().removeAttribute(
-                    FileUploadService.UPLOAD_EXCEPTION);
+        // If a POST error occurred exit early.
+        if (hasPostError()) {
+            // Remove exception to ensure other forms on Page do not
+            // validate twice for same error.
+            getContext().getRequest().removeAttribute(
+                FileUploadService.UPLOAD_EXCEPTION);
 
-                return true;
-            }
+            return true;
         }
 
         boolean continueProcessing = true;
@@ -1766,6 +1770,10 @@
                 }
             }
 
+            if (getValidate()) {
+                validate();
+            }
+
             dispatchActionEvent();
         }
 
@@ -1785,65 +1793,29 @@
     }
 
     /**
-     * Validate the Form request submission.
+     * The validate method is invoked by {@link #onProcess()} to validate
+     * the request submission. A Form subclass can override this method
+     * to implement cross-field validation logic.
      * <p/>
-     * A form error message is displayed if a validation error occurs.
-     * These messages are defined in the resource bundle:
-     * <blockquote>
-     * <ul>
-     *   <li>/click-control.properties
-     *     <ul>
-     *       <li>file-size-limit-exceeded-error</li>
-     *       <li>post-size-limit-exceeded-error</li>
-     *     </ul>
-     *   </li>
-     * </ul>
-     * </blockquote>
+     * If the Form determines that the submission is invalid it should set the
+     * {@link #error} property with an appropriate error message. For example:
+     *
+     * <pre class="prettyprint">
+     * public class RegistrationForm extends Form {
+     *
+     *     // Add validation to ensure the password and confirmPassword fields match
+     *     public void validate() {
+     *         String password = getFieldValue("password");
+     *         String confirmPassword = getFieldValue("confirmPassword");
+     *         if (!password.equals(confirmPassword)) {
+     *
+     *             // Set Form's error property value that will be shown to the user
+     *             setError("The passwords do not match.");
+     *         }
+     *     }
+     * } </pre>
      */
     public void validate() {
-        setError(null);
-
-        Exception exception = (Exception) getContext().getRequest()
-            .getAttribute(FileUploadService.UPLOAD_EXCEPTION);
-
-        if (!(exception instanceof FileUploadException)) {
-            return;
-        }
-
-        FileUploadException fue = (FileUploadException) exception;
-
-        String key = null;
-        Object args[] = null;
-
-        if (fue instanceof SizeLimitExceededException) {
-            SizeLimitExceededException se =
-                (SizeLimitExceededException) fue;
-
-            key = "post-size-limit-exceeded-error";
-
-            args = new Object[2];
-            args[0] = new Long(se.getPermittedSize());
-            args[1] = new Long(se.getActualSize());
-            setError(getMessage(key, args));
-
-        } else if (fue instanceof FileSizeLimitExceededException) {
-            FileSizeLimitExceededException fse =
-                (FileSizeLimitExceededException) fue;
-
-            key = "file-size-limit-exceeded-error";
-
-            // Parse the FileField name from the message
-            String msg = fue.getMessage();
-            int start = 10;
-            int end = msg.indexOf(' ', start);
-            String fieldName = fue.getMessage().substring(start, end);
-
-            args = new Object[3];
-            args[0] = ClickUtils.toLabel(fieldName);
-            args[1] = new Long(fse.getPermittedSize());
-            args[2] = new Long(fse.getActualSize());
-            setError(getMessage(key, args));
-        }
     }
 
     /**
@@ -2708,6 +2680,68 @@
         return false;
     }
 
+    /**
+     * Validate the request for any file upload (multipart) errors.
+     * <p/>
+     * A form error message is displayed if a file upload error occurs.
+     * These messages are defined in the resource bundle:
+     * <blockquote>
+     * <ul>
+     *   <li>/click-control.properties
+     *     <ul>
+     *       <li>file-size-limit-exceeded-error</li>
+     *       <li>post-size-limit-exceeded-error</li>
+     *     </ul>
+     *   </li>
+     * </ul>
+     * </blockquote>
+     */
+    protected void validateFileUpload() {
+        setError(null);
+
+        Exception exception = (Exception) getContext().getRequest()
+            .getAttribute(FileUploadService.UPLOAD_EXCEPTION);
+
+        if (!(exception instanceof FileUploadException)) {
+            return;
+        }
+
+        FileUploadException fue = (FileUploadException) exception;
+
+        String key = null;
+        Object args[] = null;
+
+        if (fue instanceof SizeLimitExceededException) {
+            SizeLimitExceededException se =
+                (SizeLimitExceededException) fue;
+
+            key = "post-size-limit-exceeded-error";
+
+            args = new Object[2];
+            args[0] = new Long(se.getPermittedSize());
+            args[1] = new Long(se.getActualSize());
+            setError(getMessage(key, args));
+
+        } else if (fue instanceof FileSizeLimitExceededException) {
+            FileSizeLimitExceededException fse =
+                (FileSizeLimitExceededException) fue;
+
+            key = "file-size-limit-exceeded-error";
+
+            // Parse the FileField name from the message
+            String msg = fue.getMessage();
+            int start = 10;
+            int end = msg.indexOf(' ', start);
+            String fieldName = fue.getMessage().substring(start, end);
+
+            args = new Object[3];
+            args[0] = ClickUtils.toLabel(fieldName);
+            args[1] = new Long(fse.getPermittedSize());
+            args[2] = new Long(fse.getActualSize());
+            setError(getMessage(key, args));
+        }
+    }
+
     // -------------------------------------------------------- Private Methods
 
     /**



Mime
View raw message