Skip to content

Commit

Permalink
feat: allow disabling validation status change listener registration …
Browse files Browse the repository at this point in the history
…on binder (#14198)

Add binder-level flag to allow disabling of 
validation status change listener registration 
for HasValidator fields.

Related to #13940 (comment) and #13940 (comment)

Co-authored-by: Knoobie <[email protected]>
  • Loading branch information
vaadin-bot and knoobie authored Jul 21, 2022
1 parent 9ea1e60 commit 9869b07
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
29 changes: 28 additions & 1 deletion flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,8 @@ public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder,
onValueChange = getField().addValueChangeListener(
event -> handleFieldValueChange(event));

if (getField() instanceof HasValidator) {
if (getBinder().isFieldsValidationStatusChangeListenerEnabled()
&& getField() instanceof HasValidator) {
HasValidator<FIELDVALUE> hasValidatorField = (HasValidator<FIELDVALUE>) getField();
onValidationStatusChange = hasValidatorField
.addValidationStatusChangeListener(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 9869b07

Please sign in to comment.