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 all commits
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
65 changes: 58 additions & 7 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,39 @@ 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 -> !(binding.getField() instanceof Component c)
|| c.isVisible();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the previous version looked way more readable, this feels like brain training :D

}

/**
* 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 +1271,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 +1646,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,8 +2392,8 @@ 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
Expand Down Expand Up @@ -2413,13 +2461,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 +2703,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
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,61 @@ 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 writeBean_isAppliedIsEvaluated() {
AtomicInteger invokes = new AtomicInteger();

binder.forField(nameField).withValidator(name -> false, "")
.bind(Person::getFirstName, Person::setFirstName)
.setIsAppliedPredicate(p -> {
invokes.incrementAndGet();
return false;
});

binder.readBean(item);
nameField.setValue("");

binder.writeBeanIfValid(item);
Assert.assertEquals("writeBeanIfValid should have invoked isApplied", 1,
invokes.get());

binder.writeBeanAsDraft(item);
Assert.assertEquals("writeBeanAsDraft should have invoked isApplied", 2,
invokes.get());

}

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