aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/ui/AbstractDateField.java
diff options
context:
space:
mode:
authorTatu Lund <tatu@vaadin.com>2021-02-19 11:11:49 +0200
committerGitHub <noreply@github.com>2021-02-19 11:11:49 +0200
commit551177326f6ae8c4ddd7d5b3ab3bb3311304cc0a (patch)
tree9f370578832547243f8f516b7f2b30ce79fb1194 /server/src/main/java/com/vaadin/ui/AbstractDateField.java
parent45ed0c173aedb8c0b7194ed0b8eb656d24ddac9d (diff)
downloadvaadin-framework-551177326f6ae8c4ddd7d5b3ab3bb3311304cc0a.tar.gz
vaadin-framework-551177326f6ae8c4ddd7d5b3ab3bb3311304cc0a.zip
Add API to prevent invalid input when integrated range validator is used (#12168)
Do not fire DateField's value change event if the new date is not within the range. Fixes: #12163
Diffstat (limited to 'server/src/main/java/com/vaadin/ui/AbstractDateField.java')
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractDateField.java35
1 files changed, 34 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/ui/AbstractDateField.java b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
index dbbc19b673..d14393c541 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractDateField.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractDateField.java
@@ -160,7 +160,14 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
}
}
} else {
- setValue(newDate, true);
+ RangeValidator<T> validator = getRangeValidator();
+ ValidationResult result = validator.apply(newDate,
+ new ValueContext());
+ if (!isPreventInvalidInput() || !result.isError()) {
+ setValue(newDate, true);
+ } else {
+ doSetValue(newDate);
+ }
}
}
}
@@ -229,6 +236,8 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
private String dateOutOfRangeMessage = "Date is out of allowed range";
+ private boolean preventInvalidInput = false;
+
/* Constructors */
/**
@@ -1130,4 +1139,28 @@ public abstract class AbstractDateField<T extends Temporal & TemporalAdjuster &
public void getAssistiveLabel(AccessibleElement element) {
getState(false).assistiveLabels.get(element);
}
+
+ /**
+ * Control whether value change event is emitted when user input value
+ * does not meet the integrated range validator.
+ *
+ * @param preventInvalidInput Set to false to disable the value change event.
+ *
+ * @since 8.13
+ */
+ public void setPreventInvalidInput(boolean preventInvalidInput) {
+ this.preventInvalidInput = preventInvalidInput;
+ }
+
+ /**
+ * Check whether value change is emitted when user input value does
+ * not meet integrated range validator. The default is false.
+ *
+ * @return a Boolean value
+ *
+ * @since 8.13
+ */
+ public boolean isPreventInvalidInput() {
+ return preventInvalidInput;
+ }
}