]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes #1708
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>
Thu, 22 May 2008 17:56:47 +0000 (17:56 +0000)
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>
Thu, 22 May 2008 17:56:47 +0000 (17:56 +0000)
svn changeset:4616/svn branch:trunk

src/com/itmill/toolkit/ui/AbstractField.java
src/com/itmill/toolkit/ui/TextField.java

index 9b4ea1fe91e4cf69c9bb4ddfc06283034601c98b..d9883ba3da0c896dbaf27ecd31f9271c91281c6c 100644 (file)
@@ -591,6 +591,16 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      */
     public boolean isValid() {
 
+        if (isRequired()) {
+            if (isEmpty()) {
+                return false;
+            }
+        } else {
+            if (isEmpty()) {
+                return true;
+            }
+        }
+
         if (validators == null) {
             return true;
         }
@@ -612,6 +622,16 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      */
     public void validate() throws Validator.InvalidValueException {
 
+        if (isRequired()) {
+            if (isEmpty()) {
+                throw new Validator.InvalidValueException("");
+            }
+        } else {
+            if (isEmpty()) {
+                return;
+            }
+        }
+
         // If there is no validator, there can not be any errors
         if (validators == null) {
             return;
@@ -698,13 +718,17 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      */
     public ErrorMessage getErrorMessage() {
 
-        // Check validation errors only if automatic validation is enabled
+        // Check validation errors only if automatic validation is enabled.
+        // As an exception, no validation messages are shown for empty
+        // required fields, as in those cases user is aware of the problem.
         ErrorMessage validationError = null;
         if (isValidationVisible()) {
-            try {
-                validate();
-            } catch (Validator.InvalidValueException e) {
-                validationError = e;
+            if (!(isRequired() && isEmpty())) {
+                try {
+                    validate();
+                } catch (Validator.InvalidValueException e) {
+                    validationError = e;
+                }
             }
         }
 
@@ -969,6 +993,16 @@ public abstract class AbstractField extends AbstractComponent implements Field,
     /**
      * Is this field required. Required fields must filled by the user.
      * 
+     * If the field is required, it is visually indicated in the user interface.
+     * Furthermore, setting field to be required implicitly adds "non-empty"
+     * validator and thus isValid() == false or any isEmpty() fields. In those
+     * cases validation errors are not painted as it is obvious that the user
+     * must fill in the required fields.
+     * 
+     * On the other hand, for the non-required fields isValid() == true if the
+     * field isEmpty() regardless of any attached validators.
+     * 
+     * 
      * @return <code>true</code> if the field is required .otherwise
      *         <code>false</code>.
      */
@@ -979,6 +1013,15 @@ public abstract class AbstractField extends AbstractComponent implements Field,
     /**
      * Sets the field required. Required fields must filled by the user.
      * 
+     * If the field is required, it is visually indicated in the user interface.
+     * Furthermore, setting field to be required implicitly adds "non-empty"
+     * validator and thus isValid() == false or any isEmpty() fields. In those
+     * cases validation errors are not painted as it is obvious that the user
+     * must fill in the required fields.
+     * 
+     * On the other hand, for the non-required fields isValid() == true if the
+     * field isEmpty() regardless of any attached validators.
+     * 
      * @param required
      *                Is the field required.
      */
@@ -987,6 +1030,16 @@ public abstract class AbstractField extends AbstractComponent implements Field,
         requestRepaint();
     }
 
+    /**
+     * Is the field empty?
+     * 
+     * In general, "empty" state is same as null. As an exception, TextField
+     * also treats empty string as "empty".
+     */
+    protected boolean isEmpty() {
+        return (value == null);
+    }
+
     /**
      * Is automatic, visible validation enabled?
      * 
index 49dbe20829767f80b1f6b5d79c67db95e7c46c3e..908c0c2178d67b16d45a7b928224ca62e772360d 100644 (file)
@@ -213,8 +213,8 @@ public class TextField extends AbstractField {
      */
     public void changeVariables(Object source, Map variables) {
 
-       super.changeVariables(source, variables);
-       
+        super.changeVariables(source, variables);
+
         // Sets the text
         if (variables.containsKey("text") && !isReadOnly()) {
 
@@ -456,4 +456,8 @@ public class TextField extends AbstractField {
         this.format = format;
     }
 
+    protected boolean isEmpty() {
+        return super.isEmpty() || toString().length() == 0;
+    }
+
 }