aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/main')
-rw-r--r--server/src/main/java/com/vaadin/data/Binder.java55
1 files changed, 54 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java
index 747983bfa5..cd7b834571 100644
--- a/server/src/main/java/com/vaadin/data/Binder.java
+++ b/server/src/main/java/com/vaadin/data/Binder.java
@@ -226,6 +226,30 @@ public class Binder<BEAN> implements Serializable {
* @since 8.4
*/
public Setter<BEAN, TARGET> getSetter();
+
+ /**
+ * Enable or disable asRequired validator.
+ * The validator is enabled by default.
+ *
+ * @see #asRequired(String)
+ * @see #asRequired(ErrorMessageProvider)
+ *
+ * @param asRequiredEnabled
+ * {@code false} if asRequired validator should
+ * be disabled, {@code true} otherwise (default)
+ */
+ public void setAsRequiredEnabled(boolean asRequiredEnabled);
+
+ /**
+ * Returns whether asRequired validator is currently enabled or not
+ *
+ * @see #asRequired(String)
+ * @see #asRequired(ErrorMessageProvider)
+ *
+ * @return {@code false} if asRequired validator is disabled
+ * {@code true} otherwise (default)
+ */
+ public boolean isAsRequiredEnabled();
}
/**
@@ -781,6 +805,8 @@ public class Binder<BEAN> implements Serializable {
*/
private Converter<FIELDVALUE, ?> converterValidatorChain;
+ private boolean asRequiredSet;
+
/**
* Creates a new binding builder associated with the given field.
* Initializes the builder with the given converter chain and status
@@ -917,8 +943,14 @@ public class Binder<BEAN> implements Serializable {
public BindingBuilder<BEAN, TARGET> asRequired(
Validator<TARGET> customRequiredValidator) {
checkUnbound();
+ this.asRequiredSet = true;
field.setRequiredIndicatorVisible(true);
- return withValidator(customRequiredValidator);
+ return withValidator((value, context) -> {
+ if (!field.isRequiredIndicatorVisible())
+ return ValidationResult.ok();
+ else
+ return customRequiredValidator.apply(value, context);
+ });
}
/**
@@ -1020,12 +1052,15 @@ public class Binder<BEAN> implements Serializable {
*/
private final Converter<FIELDVALUE, TARGET> converterValidatorChain;
+ private boolean asRequiredSet;
+
public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder,
ValueProvider<BEAN, TARGET> getter,
Setter<BEAN, TARGET> setter) {
this.binder = builder.getBinder();
this.field = builder.field;
this.statusHandler = builder.statusHandler;
+ this.asRequiredSet = builder.asRequiredSet;
converterValidatorChain = ((Converter<FIELDVALUE, TARGET>) builder.converterValidatorChain);
onValueChange = getField()
@@ -1282,6 +1317,24 @@ public class Binder<BEAN> implements Serializable {
public Setter<BEAN, TARGET> getSetter() {
return setter;
}
+
+ @Override
+ public void setAsRequiredEnabled(boolean asRequiredEnabled) {
+ if (!asRequiredSet) {
+ throw new IllegalStateException(
+ "Unable to toggle asRequired validation since "
+ + "asRequired has not been set.");
+ }
+ if (asRequiredEnabled != isAsRequiredEnabled()) {
+ field.setRequiredIndicatorVisible(asRequiredEnabled);
+ validate();
+ }
+ }
+
+ @Override
+ public boolean isAsRequiredEnabled() {
+ return field.isRequiredIndicatorVisible();
+ }
}
/**