Skip to content

Commit

Permalink
fixes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
tepi committed Jan 25, 2024
1 parent 420ebc3 commit 85dd5f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
11 changes: 3 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 @@ -314,13 +314,8 @@ default boolean isApplied() {
* @return predicate for testing {@link #isApplied()}
*/
default SerializablePredicate<Binding<BEAN, TARGET>> getIsAppliedPredicate() {
return binding -> {
if (binding.getField() instanceof Component) {
return ((Component) binding.getField()).isVisible();
} else {
return true;
}
};
return binding -> !(binding.getField() instanceof Component c)
|| c.isVisible();
}

/**
Expand Down Expand Up @@ -2403,7 +2398,7 @@ private BinderValidationStatus<BEAN> doWriteIfValid(BEAN bean,
// First run fields level validation, if no validation errors then
// update bean
List<BindingValidationStatus<?>> bindingResults = currentBindings
.stream().filter(Binding::isApplied).map(b -> b.validate(false))
.stream().map(b -> b.validate(false))
.collect(Collectors.toList());

if (bindingResults.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,30 @@ public void withValidator_isAppliedIsEvaluated() {
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

0 comments on commit 85dd5f4

Please sign in to comment.