From 429176d040d79fd508080437254958a2456d71a0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marko=20Gr=C3=B6nroos?= Date: Thu, 3 Jul 2008 10:27:53 +0000 Subject: [PATCH] Fixes the main problem in #1867, but brings other problems. Needs more work. svn changeset:5021/svn branch:trunk --- src/com/itmill/toolkit/ui/AbstractField.java | 46 ++++++++++++++++---- src/com/itmill/toolkit/ui/Field.java | 18 ++++++++ src/com/itmill/toolkit/ui/Form.java | 29 ++++++++++++ 3 files changed, 85 insertions(+), 8 deletions(-) diff --git a/src/com/itmill/toolkit/ui/AbstractField.java b/src/com/itmill/toolkit/ui/AbstractField.java index 86fdb2bb4d..055b857b81 100644 --- a/src/com/itmill/toolkit/ui/AbstractField.java +++ b/src/com/itmill/toolkit/ui/AbstractField.java @@ -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? diff --git a/src/com/itmill/toolkit/ui/Field.java b/src/com/itmill/toolkit/ui/Field.java index 2da6d0ca89..9532b2cb64 100644 --- a/src/com/itmill/toolkit/ui/Field.java +++ b/src/com/itmill/toolkit/ui/Field.java @@ -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 Event object specifying the Field whose value has been * changed. diff --git a/src/com/itmill/toolkit/ui/Form.java b/src/com/itmill/toolkit/ui/Form.java index 4612befcab..0140f0dffb 100644 --- a/src/com/itmill/toolkit/ui/Form.java +++ b/src/com/itmill/toolkit/ui/Form.java @@ -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. -- 2.39.5