]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes #1661
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>
Mon, 19 May 2008 08:58:08 +0000 (08:58 +0000)
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>
Mon, 19 May 2008 08:58:08 +0000 (08:58 +0000)
svn changeset:4539/svn branch:trunk

src/com/itmill/toolkit/tests/tickets/Ticket846.java
src/com/itmill/toolkit/ui/AbstractField.java

index 8fa5bab9926bd413fd2915706749d4aa6f377db4..75066890f4cec5657ec8c760839cb0cbfc961142 100644 (file)
@@ -1,6 +1,5 @@
 package com.itmill.toolkit.tests.tickets;
 
-
 import com.itmill.toolkit.Application;
 import com.itmill.toolkit.data.Validator;
 import com.itmill.toolkit.data.util.MethodProperty;
@@ -10,45 +9,50 @@ import com.itmill.toolkit.ui.Window;
 
 public class Ticket846 extends Application {
 
-       public void init() {
-
-               final Window mainWin = new Window("Test app for #846");
-               setMainWindow(mainWin);
-
-               final TextField tx = new TextField("Integer");
-               mainWin.addComponent(tx);
-               tx.setImmediate(true);
-               tx.addValidator(new Validator() {
-
-                       public boolean isValid(Object value) {
-                               try {
-                                       Integer.parseInt("" + value);
-                                       return true;
-                               } catch (NumberFormatException e) {
-                                       return false;
-                               }
-                       }
-
-                       public void validate(Object value) throws InvalidValueException {
-                               if (!isValid(value))
-                                       throw new InvalidValueException(value + " is not a number");
-                       }
-               });
-               
-               final String[] visibleProps = {"required","invalidAllowed","readOnly","readThrough","invalidCommitted"}; 
-               for (int i=0;i<visibleProps.length; i++ ) {
-                       Button b = new Button(visibleProps[i],new MethodProperty(tx,visibleProps[i]));
-                       b.setImmediate(true);
-                       mainWin.addComponent(b);
-               }
-
-               mainWin.addComponent(new Button("Validate integer",
-                               new Button.ClickListener() {
-                                       public void buttonClick(
-                                                       com.itmill.toolkit.ui.Button.ClickEvent event) {
-                                               mainWin.showNotification("The field is " + (tx.isValid()?"":"not ") + "valid");
-                                       };
-                               }));
-       }
+    public void init() {
+
+        final Window mainWin = new Window("Test app for #846");
+        setMainWindow(mainWin);
+
+        final TextField tx = new TextField("Integer");
+        mainWin.addComponent(tx);
+        tx.setImmediate(true);
+        tx.addValidator(new Validator() {
+
+            public boolean isValid(Object value) {
+                try {
+                    Integer.parseInt("" + value);
+                    return true;
+                } catch (NumberFormatException e) {
+                    return false;
+                }
+            }
+
+            public void validate(Object value) throws InvalidValueException {
+                if (!isValid(value)) {
+                    throw new InvalidValueException(value + " is not a number");
+                }
+            }
+        });
+
+        final String[] visibleProps = { "required", "invalidAllowed",
+                "readOnly", "readThrough", "invalidCommitted",
+                "validationVisible" };
+        for (int i = 0; i < visibleProps.length; i++) {
+            Button b = new Button(visibleProps[i], new MethodProperty(tx,
+                    visibleProps[i]));
+            b.setImmediate(true);
+            mainWin.addComponent(b);
+        }
+
+        mainWin.addComponent(new Button("Validate integer",
+                new Button.ClickListener() {
+                    public void buttonClick(
+                            com.itmill.toolkit.ui.Button.ClickEvent event) {
+                        mainWin.showNotification("The field is "
+                                + (tx.isValid() ? "" : "not ") + "valid");
+                    };
+                }));
+    }
 
 }
index 7fef394a976bb0cb8762c4469bfa277409680529..fb251d70b692e3d2d04529dd8af3be4ad158dd58 100644 (file)
@@ -110,6 +110,11 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      */
     private boolean required = false;
 
+    /**
+     * Is automatic validation enabled.
+     */
+    private boolean validationVisible = true;
+
     /* Component basics ************************************************ */
 
     /*
@@ -691,12 +696,14 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      */
     public ErrorMessage getErrorMessage() {
 
-        // Check validation errors
+        // Check validation errors only if automatic validation is enabled
         ErrorMessage validationError = null;
-        try {
-            validate();
-        } catch (Validator.InvalidValueException e) {
-            validationError = e;
+        if (isValidationVisible()) {
+            try {
+                validate();
+            } catch (Validator.InvalidValueException e) {
+                validationError = e;
+            }
         }
 
         // Check if there are any systems errors
@@ -976,4 +983,38 @@ public abstract class AbstractField extends AbstractComponent implements Field,
         requestRepaint();
     }
 
+    /**
+     * Is automatic, visible validation enabled?
+     * 
+     * If automatic validation is enabled, any validators connected to this
+     * component are evaluated while painting the component and potential error
+     * messages are sent to client. If the automatic validation is turned off,
+     * isValid() and validate() methods still work, but one must show the
+     * validation in their own code.
+     * 
+     * @return True, if automatic validation is enabled.
+     */
+    public boolean isValidationVisible() {
+        return validationVisible;
+    }
+
+    /**
+     * Enable or disable automatic, visible validation.
+     * 
+     * If automatic validation is enabled, any validators connected to this
+     * component are evaluated while painting the component and potential error
+     * messages are sent to client. If the automatic validation is turned off,
+     * isValid() and validate() methods still work, but one must show the
+     * validation in their own code.
+     * 
+     * @param validateAutomatically
+     *                True, if automatic validation is enabled.
+     */
+    public void setValidationVisible(boolean validateAutomatically) {
+        if (validationVisible != validateAutomatically) {
+            requestRepaint();
+            validationVisible = validateAutomatically;
+        }
+    }
+
 }
\ No newline at end of file