From 79fc9b23a241e1e3e335c7bc7a1f78f049c52ec7 Mon Sep 17 00:00:00 2001 From: Joonas Lehtinen Date: Thu, 22 May 2008 17:56:47 +0000 Subject: [PATCH] Fixes #1708 svn changeset:4616/svn branch:trunk --- src/com/itmill/toolkit/ui/AbstractField.java | 63 ++++++++++++++++++-- src/com/itmill/toolkit/ui/TextField.java | 8 ++- 2 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/com/itmill/toolkit/ui/AbstractField.java b/src/com/itmill/toolkit/ui/AbstractField.java index 9b4ea1fe91..d9883ba3da 100644 --- a/src/com/itmill/toolkit/ui/AbstractField.java +++ b/src/com/itmill/toolkit/ui/AbstractField.java @@ -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 true if the field is required .otherwise * false. */ @@ -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? * diff --git a/src/com/itmill/toolkit/ui/TextField.java b/src/com/itmill/toolkit/ui/TextField.java index 49dbe20829..908c0c2178 100644 --- a/src/com/itmill/toolkit/ui/TextField.java +++ b/src/com/itmill/toolkit/ui/TextField.java @@ -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; + } + } -- 2.39.5