Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds isApplied feature for Binding #18540

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 64 additions & 8 deletions flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,44 @@ default BindingValidationStatus<TARGET> validate() {
* changes, otherwise {@literal false}.
*/
boolean hasChanges();

/**
* Checks whether this binding should be processed during validation and
* writing to bean.
*
* @return {@literal true} if this binding should be processed,
* {@literal false} if this binding should be ignored
*/
default boolean isApplied() {
return getIsAppliedPredicate().test(this);
}

/**
* Gets predicate for testing {@link #isApplied()}. By default,
* non-visible components are ignored during validation and bean
* writing.
*
* @return predicate for testing {@link #isApplied()}
*/
default SerializablePredicate<Binding<BEAN, TARGET>> getIsAppliedPredicate() {
return binding -> {
if (binding.getField() instanceof Component) {
return ((Component) binding.getField()).isVisible();
tepi marked this conversation as resolved.
Show resolved Hide resolved
} else {
return true;
}
};
}

/**
* Sets a custom predicate for testing {@link #isApplied()}. Set to
* {@literal null} to restore default functionality.
*
* @param isAppliedPredicate
* custom predicate for testing {@link #isApplied()}
*/
void setIsAppliedPredicate(
SerializablePredicate<Binding<BEAN, TARGET>> isAppliedPredicate);
}

/**
Expand Down Expand Up @@ -1238,6 +1276,8 @@ protected static class BindingImpl<BEAN, FIELDVALUE, TARGET>

private Registration onValidationStatusChange;

private SerializablePredicate<Binding<BEAN, TARGET>> isAppliedPredicate;

public BindingImpl(BindingBuilderImpl<BEAN, FIELDVALUE, TARGET> builder,
ValueProvider<BEAN, TARGET> getter,
Setter<BEAN, TARGET> setter) {
Expand Down Expand Up @@ -1611,6 +1651,19 @@ public boolean hasChanges() throws IllegalStateException {

return this.binder.hasChanges(this);
}

@Override
public SerializablePredicate<Binding<BEAN, TARGET>> getIsAppliedPredicate() {
return isAppliedPredicate == null
? Binding.super.getIsAppliedPredicate()
: isAppliedPredicate;
}

@Override
public void setIsAppliedPredicate(
SerializablePredicate<Binding<BEAN, TARGET>> isAppliedPredicate) {
this.isAppliedPredicate = isAppliedPredicate;
}
}

/**
Expand Down Expand Up @@ -2344,13 +2397,13 @@ private BinderValidationStatus<BEAN> doWriteIfValid(BEAN bean,

// make a copy of the incoming bindings to avoid their modifications
// during validation
Collection<Binding<BEAN, ?>> currentBindings = new ArrayList<>(
bindings);
Collection<Binding<BEAN, ?>> currentBindings = bindings.stream()
.filter(Binding::isApplied).collect(Collectors.toList());

// First run fields level validation, if no validation errors then
// update bean
List<BindingValidationStatus<?>> bindingResults = currentBindings
.stream().map(b -> b.validate(false))
.stream().filter(Binding::isApplied).map(b -> b.validate(false))
tepi marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toList());

if (bindingResults.stream()
Expand Down Expand Up @@ -2413,13 +2466,15 @@ private void doWriteDraft(BEAN bean, Collection<Binding<BEAN, ?>> bindings,
Objects.requireNonNull(bean, "bean cannot be null");

if (!forced) {
bindings.forEach(binding -> ((BindingImpl<BEAN, ?, ?>) binding)
.writeFieldValue(bean));
bindings.stream().filter(Binding::isApplied)
.forEach(binding -> ((BindingImpl<BEAN, ?, ?>) binding)
.writeFieldValue(bean));
} else {
boolean isDisabled = isValidatorsDisabled();
setValidatorsDisabled(true);
bindings.forEach(binding -> ((BindingImpl<BEAN, ?, ?>) binding)
.writeFieldValue(bean));
bindings.stream().filter(Binding::isApplied)
.forEach(binding -> ((BindingImpl<BEAN, ?, ?>) binding)
.writeFieldValue(bean));
setValidatorsDisabled(isDisabled);
}
}
Expand Down Expand Up @@ -2653,7 +2708,8 @@ public boolean isValid() {
* @return an immutable list of validation results for bindings
*/
private List<BindingValidationStatus<?>> validateBindings() {
return getBindings().stream().map(BindingImpl::doValidation)
return getBindings().stream().filter(Binding::isApplied)
.map(BindingImpl::doValidation)
.collect(Collectors.collectingAndThen(Collectors.toList(),
Collections::unmodifiableList));
}
Expand Down
tepi marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.data.binder.Binder.Binding;
Expand Down Expand Up @@ -754,6 +755,37 @@ public void withValidator_doesNotDisablesDefaulNullRepresentation() {
Assert.assertEquals(newValue, item.getFirstName());
}

@Test
public void withValidator_isAppliedIsEvaluated() {
// Binding has validator which always fails
Binding<Person, String> nameBinding = binder.forField(nameField)
.withValidator(name -> false, "")
.bind(Person::getFirstName, Person::setFirstName);
binder.setBean(item);

// Base state -> not valid
Assert.assertFalse(binder.isValid());

// Default -> non-visible field -> valid
((Component) nameBinding.getField()).setVisible(false);
Assert.assertTrue(binder.isValid());

// isApplied = false -> valid
((Component) nameBinding.getField()).setVisible(true);
nameBinding.setIsAppliedPredicate(p -> false);
Assert.assertTrue(binder.isValid());

// isApplied = true -> not valid
nameBinding.setIsAppliedPredicate(p -> true);
Assert.assertFalse(binder.isValid());

// Check removing predicate and restoring default behavior
nameBinding.setIsAppliedPredicate(null);
Assert.assertFalse(binder.isValid());
((Component) nameBinding.getField()).setVisible(false);
Assert.assertTrue(binder.isValid());
}

@Test
public void setRequired_withErrorMessage_fieldGetsRequiredIndicatorAndValidator() {
TestTextField textField = new TestTextField();
Expand Down
Loading