From 9869b07ff4e15b74ca4a112e4e86b41e886b2041 Mon Sep 17 00:00:00 2001 From: Vaadin Bot Date: Thu, 21 Jul 2022 14:45:34 +0200 Subject: [PATCH] feat: allow disabling validation status change listener registration on binder (#14198) Add binder-level flag to allow disabling of validation status change listener registration for HasValidator fields. Related to https://github.com/vaadin/flow/pull/13940#issuecomment-1174668400 and https://github.com/vaadin/flow/pull/13940#issuecomment-1174668400 Co-authored-by: Knoobie --- .../com/vaadin/flow/data/binder/Binder.java | 29 ++++++++++++++++++- ...derValidationStatusChangeListenerTest.java | 21 ++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java index d8cc01d5684..fa2222a98e0 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java @@ -1221,7 +1221,8 @@ public BindingImpl(BindingBuilderImpl builder, onValueChange = getField().addValueChangeListener( event -> handleFieldValueChange(event)); - if (getField() instanceof HasValidator) { + if (getBinder().isFieldsValidationStatusChangeListenerEnabled() + && getField() instanceof HasValidator) { HasValidator hasValidatorField = (HasValidator) getField(); onValidationStatusChange = hasValidatorField .addValidationStatusChangeListener( @@ -1679,6 +1680,8 @@ void setIdentity() { private boolean validatorsDisabled = false; + private boolean fieldsValidationStatusChangeListenerEnabled = true; + /** * Creates a binder using a custom {@link PropertySet} implementation for * finding and resolving property names for @@ -3448,6 +3451,30 @@ public boolean isValidatorsDisabled() { return validatorsDisabled; } + /** + * Control whether bound fields implementing {@link HasValidator} subscribe + * for field's {@code ValidationStatusChangeEvent}s and will + * {@code validate} upon receiving them. + * + * @param fieldsValidationStatusChangeListenerEnabled + * Boolean value. + */ + public void setFieldsValidationStatusChangeListenerEnabled( + boolean fieldsValidationStatusChangeListenerEnabled) { + this.fieldsValidationStatusChangeListenerEnabled = fieldsValidationStatusChangeListenerEnabled; + } + + /** + * Returns if the bound fields implementing {@link HasValidator} subscribe + * for field's {@code ValidationStatusChangeEvent}s and will + * {@code validate} upon receiving them. + * + * @return Boolean value + */ + public boolean isFieldsValidationStatusChangeListenerEnabled() { + return fieldsValidationStatusChangeListenerEnabled; + } + /** * Sets a {@code handler} to customize the {@link RuntimeException} thrown * by delegates (like {@link Setter}, {@link ValueProvider}, diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderValidationStatusChangeListenerTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderValidationStatusChangeListenerTest.java index 92511bb6d21..0ba93d4747a 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderValidationStatusChangeListenerTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderValidationStatusChangeListenerTest.java @@ -64,6 +64,16 @@ public void fieldWithHasValidatorDefaults_bindIsCalled_addValidationStatusListen .addValidationStatusChangeListener(Mockito.any()); } + @Test + public void binderWithFieldsValidationStatusChangeListenerDisabled_bindIsCalled_noValidationStatusListenerIsCalled() { + binder.setFieldsValidationStatusChangeListenerEnabled(false); + var field = Mockito.spy( + TestHasValidatorDatePicker.DatePickerHasValidatorDefaults.class); + binder.bind(field, BIRTH_DATE_PROPERTY); + Mockito.verify(field, Mockito.never()) + .addValidationStatusChangeListener(Mockito.any()); + } + @Test public void fieldWithHasValidatorOnlyGetDefaultValidatorOverridden_bindIsCalled_addValidationStatusListenerIsCalled() { var field = Mockito.spy( @@ -102,6 +112,17 @@ public void fieldWithHasValidatorFullyOverridden_fieldValidationStatusChangesToF Assert.assertEquals(INVALID_DATE_FORMAT, componentErrors.get(field)); } + @Test + public void binderWithFieldsValidationStatusChangeListenerDisabled_fieldValidationStatusChangesToFalse_binderHandleErrorIsNotCalled() { + binder.setFieldsValidationStatusChangeListenerEnabled(false); + var field = new TestHasValidatorDatePicker.DataPickerHasValidatorOverridden(); + binder.bind(field, BIRTH_DATE_PROPERTY); + Assert.assertEquals(0, componentErrors.size()); + + field.fireValidationStatusChangeEvent(false); + Assert.assertEquals(0, componentErrors.size()); + } + @Test public void fieldWithHasValidatorFullyOverridden_fieldValidationStatusChangesToTrue_binderClearErrorIsCalled() { var field = new TestHasValidatorDatePicker.DataPickerHasValidatorOverridden();