aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>2008-05-22 17:56:47 +0000
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>2008-05-22 17:56:47 +0000
commit79fc9b23a241e1e3e335c7bc7a1f78f049c52ec7 (patch)
tree15a80d63335c5d840a957ee0101f7c74ec631b8a /src
parentd5f687e49e3fadb5f6f25bc5b5f9162e56864a20 (diff)
downloadvaadin-framework-79fc9b23a241e1e3e335c7bc7a1f78f049c52ec7.tar.gz
vaadin-framework-79fc9b23a241e1e3e335c7bc7a1f78f049c52ec7.zip
Fixes #1708
svn changeset:4616/svn branch:trunk
Diffstat (limited to 'src')
-rw-r--r--src/com/itmill/toolkit/ui/AbstractField.java63
-rw-r--r--src/com/itmill/toolkit/ui/TextField.java8
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 <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.
*/
@@ -988,6 +1031,16 @@ public abstract class AbstractField extends AbstractComponent implements Field,
}
/**
+ * 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?
*
* If automatic validation is enabled, any validators connected to this
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;
+ }
+
}