]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes the main problem in #1867, but brings other problems. Needs more work.
authorMarko Grönroos <magi@iki.fi>
Thu, 3 Jul 2008 10:27:53 +0000 (10:27 +0000)
committerMarko Grönroos <magi@iki.fi>
Thu, 3 Jul 2008 10:27:53 +0000 (10:27 +0000)
svn changeset:5021/svn branch:trunk

src/com/itmill/toolkit/ui/AbstractField.java
src/com/itmill/toolkit/ui/Field.java
src/com/itmill/toolkit/ui/Form.java

index 86fdb2bb4dbd359b0d4ef87f219a1554716debf8..055b857b8193f3fdb987a3877cd955b910df3198 100644 (file)
@@ -109,6 +109,12 @@ public abstract class AbstractField extends AbstractComponent implements Field,
      * Required field.
      */
     private boolean required = false;
+    
+    /**
+     * The error message for the exception that is thrown when the field is
+     * required but empty.
+     */
+    private String requiredError = "";
 
     /**
      * Is automatic validation enabled.
@@ -610,9 +616,26 @@ public abstract class AbstractField extends AbstractComponent implements Field,
 
         return true;
     }
+    
+    public class EmptyValueException extends Validator.InvalidValueException {
+        /**
+         * Serial generated by eclipse.
+         */
+        private static final long serialVersionUID = -4488988853486652602L;
+
+        public EmptyValueException(String message) {
+            super(message);
+        }
+        
+    }
 
     /**
-     * Checks the validity of the validatable
+     * Checks the validity of the Validatable by validating the field with all
+     * attached validators.
+     * 
+     * The "required" validation is a built-in validation feature. If the field
+     * is required, but empty, validation will throw an EmptyValueException
+     * with the error message set with setRequiredError().
      * 
      * @see com.itmill.toolkit.data.Validatable#validate()
      */
@@ -620,7 +643,7 @@ public abstract class AbstractField extends AbstractComponent implements Field,
 
         if (isRequired()) {
             if (isEmpty()) {
-                throw new Validator.InvalidValueException("");
+                throw new EmptyValueException(requiredError);
             }
         }
 
@@ -715,12 +738,10 @@ public abstract class AbstractField extends AbstractComponent implements Field,
         // required fields, as in those cases user is aware of the problem.
         ErrorMessage validationError = null;
         if (isValidationVisible()) {
-            if (!(isRequired() && isEmpty())) {
-                try {
-                    validate();
-                } catch (Validator.InvalidValueException e) {
-                    validationError = e;
-                }
+            try {
+                validate();
+            } catch (Validator.InvalidValueException e) {
+                validationError = e;
             }
         }
 
@@ -1021,6 +1042,15 @@ public abstract class AbstractField extends AbstractComponent implements Field,
         this.required = required;
         requestRepaint();
     }
+    
+    public void setRequiredError(String requiredMessage) {
+        this.requiredError = requiredMessage;
+        requestRepaint();
+    }
+
+    public String getRequiredError() {
+        return requiredError;
+    }
 
     /**
      * Is the field empty?
index 2da6d0ca89dc68412cd0a5d33e07ae101b39eb0e..9532b2cb64f4c40b17f7d1a6bb4f597a602fc93b 100644 (file)
@@ -52,6 +52,24 @@ public interface Field extends Component, BufferedValidatable, Property,
      */
     public void setRequired(boolean required);
 
+    /**
+     * Sets the error message to be displayed if a required field is empty.
+     * 
+     * @param requiredMessage
+     *            Error message.
+     * @since 5.2.6
+     */
+    public void setRequiredError(String requiredMessage);
+
+    /**
+     * Gets the error message that is to be displayed if a required field is
+     * empty.
+     * 
+     * @return Error message.
+     * @since 5.2.6
+     */
+    public String getRequiredError();
+    
     /**
      * An <code>Event</code> object specifying the Field whose value has been
      * changed.
index 4612befcabf93f314e78ed955c0a09f25127071a..0140f0dffb8a6dc3af6bcd89bb763022ca4f1ff5 100644 (file)
@@ -17,6 +17,7 @@ import com.itmill.toolkit.data.Validatable;
 import com.itmill.toolkit.data.Validator;
 import com.itmill.toolkit.data.Validator.InvalidValueException;
 import com.itmill.toolkit.data.util.BeanItem;
+import com.itmill.toolkit.terminal.ErrorMessage;
 import com.itmill.toolkit.terminal.PaintException;
 import com.itmill.toolkit.terminal.PaintTarget;
 
@@ -170,6 +171,34 @@ public class Form extends AbstractField implements Item.Editor, Buffered, Item,
         }
     }
 
+    /**
+     * The error message of a Form is the error of the first field with a
+     * non-empty error.
+     * 
+     * Empty error messages of the contained fields are skipped, because an
+     * empty error indicator would be confusing to the user, especially if there
+     * are errors that have something to display. This is also the reason why
+     * the calculation of the error message is separate from validation, because
+     * validation fails also on empty errors.
+     */
+    @SuppressWarnings("unchecked")
+    @Override
+    public ErrorMessage getErrorMessage() {
+        for (final Iterator i = propertyIds.iterator(); i.hasNext();) {
+            try {
+                AbstractComponent field = (AbstractComponent)fields.get(i.next());
+                ErrorMessage e = field.getErrorMessage();
+                if (e != null) {
+                    // Skip empty errors
+                    if (e.toString().isEmpty())
+                        continue;
+                    return e;
+                }
+            } catch (ClassCastException ignored) {}
+        }
+        return null;
+    }
+
     /*
      * Commit changes to the data source Don't add a JavaDoc comment here, we
      * use the default one from the interface.