aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/ui/AbstractField.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/itmill/toolkit/ui/AbstractField.java')
-rw-r--r--src/com/itmill/toolkit/ui/AbstractField.java63
1 files changed, 58 insertions, 5 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