From 252ef116342a6b0363b48d157de33a932d44752f Mon Sep 17 00:00:00 2001 From: Tatu Lund Date: Mon, 2 Dec 2019 11:08:01 +0200 Subject: Make asRequired conditional on binding.setAsRequiredEnabled(..) (#11834) It is a very common use case in complex form that whether a field is required or not, it depends on input on other fields. Hypothetical use case sample could be that we have form for a Product and price of the product is needed except in case the Product's type is Sample. So in that kind of scenarios it would be needed to turn off asRequired() validation easily. The purpose of this enhancement and new binding.setAsRequiredEnabled(..) API is to help implementation of this kind of use cases more easily. https://github.com/vaadin/framework/issues/10709 --- server/src/main/java/com/vaadin/data/Binder.java | 55 +++++++++++++++++++++- .../src/test/java/com/vaadin/data/BinderTest.java | 9 ++-- 2 files changed, 60 insertions(+), 4 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 implements Serializable { * @since 8.4 */ public Setter 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 implements Serializable { */ private Converter 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 implements Serializable { public BindingBuilder asRequired( Validator 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 implements Serializable { */ private final Converter converterValidatorChain; + private boolean asRequiredSet; + public BindingImpl(BindingBuilderImpl builder, ValueProvider getter, Setter setter) { this.binder = builder.getBinder(); this.field = builder.field; this.statusHandler = builder.statusHandler; + this.asRequiredSet = builder.asRequiredSet; converterValidatorChain = ((Converter) builder.converterValidatorChain); onValueChange = getField() @@ -1282,6 +1317,24 @@ public class Binder implements Serializable { public Setter 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(); + } } /** diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java index 401cc07017..b3fe5a8194 100644 --- a/server/src/test/java/com/vaadin/data/BinderTest.java +++ b/server/src/test/java/com/vaadin/data/BinderTest.java @@ -473,13 +473,13 @@ public class BinderTest extends BinderTestBase, Person> { TextField textField = new TextField(); assertFalse(textField.isRequiredIndicatorVisible()); - BindingBuilder binding = binder.forField(textField); + BindingBuilder bindingBuilder = binder.forField(textField); assertFalse(textField.isRequiredIndicatorVisible()); - binding.asRequired("foobar"); + bindingBuilder.asRequired("foobar"); assertTrue(textField.isRequiredIndicatorVisible()); - binding.bind(Person::getFirstName, Person::setFirstName); + Binding binding = bindingBuilder.bind(Person::getFirstName, Person::setFirstName); binder.setBean(item); assertNull(textField.getErrorMessage()); @@ -491,6 +491,9 @@ public class BinderTest extends BinderTestBase, Person> { textField.setValue("value"); assertNull(textField.getErrorMessage()); assertTrue(textField.isRequiredIndicatorVisible()); + + binding.setAsRequiredEnabled(false); + assertFalse(textField.isRequiredIndicatorVisible()); } @Test -- cgit v1.2.3