flow: Binder setter method is not invoked until the entire form is completed

Description of the bug

With recent updates to the form validation process, the binder setter method is now not invoked until the entire form is completed.

For example, in the following code, I set the name value to DTO:

        bindingBuilder.bind(dto -> value, (dto, name) -> {
            name = StringUtils.isNoneBlank(name) ? name.trim() : null;
            dto.setName(name);
        });

And then, in another field, I check the name for some custom validation. For example:

binder.forField(skillComboBox).withValidator(entity -> {
                    JobableDto dto = binder.getBean();
                    if(dto.getName ... some logic

Now I’m unable to get the dto.getName() value until I completely fill out the entire form and press Save button with binder.writeBean(dto). Previously, I was able to see these values without that.

Is there a way to achieve this with the current implementation?

This is not very convenient, as it requires the user to wait until the entire form is completed before being notified of any additional validation errors

Expected behavior

The binder setter method should be invoked immediately after the field value is added and validated on the filed level.

Minimal reproducible example

n/a

Versions

  • Vaadin / Flow version: 24.4.0.alpha8
  • Java version: 17

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 28 (24 by maintainers)

Most upvoted comments

But the good thing is that we can start by adding new API to make it configurable and preserving the default and then we have the option of changing the default in a later release.

I’ll create an enhancement ticket and reference this one

Nothing significant for Vaadin 24.4 but there have been various tweaks throughout the 24.x releases. Difficult to guess if there’s any specific improvement that would be particularly impactful without knowing details about your case.

Additionally, need to enhance tests for flow-data module. Running module unit tests with either current code or reverted code passes. Same is true for when I replace BinderTest with a version from before these changes. So apparently we do not and did not have any test that would check this part of setBean and bean-writing functionality.

We have a few days until beta, so I’d suggest reverting change here https://github.com/vaadin/flow/blob/main/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java#L1930-L1946 to essentially this https://github.com/vaadin/flow/blob/23.2/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java#L1810-L1814. That would restore previous functionality, the other changes can be kept.

Also, need to ensure Javadocs reflect the actual functionality reverted to. Probably also would not hurt to add to Javadocs that despite it’s name, BinderValidationStatus can in these cases mean the validation status of a single binding, so it should not be relied on for Binder’s validation state in cases setBean is used.

If we want to make it configurable, then I’m wondering if it would be better to introduce that as an explicit setThisAndThat(boolean) mode or if it would be better to introduce separate variants of setBean that work in different ways?

Thinking out load without much thoughs:

Having a overloaded method for setBean(Object bean, BeanValidationType validationType) with the modes (BeanValidationType.BINDING_ONLY, BeanValidationType.BEAN) would be my first suggestion, where BINDING_ONLY would be the default / old way of things, and BEAN the alternative where everything has to be valid before the bean is changed.

I personally would go with 1. - because I can understand why you guys wanted to change the way how it currently works; see Teppo’s comment here https://github.com/vaadin/flow/issues/18735#issuecomment-1952117463 - but sadly the old behavior was there for years(?) and kinda accepted and relied upon; even tho you could say it’s a bug (even tho a convenient and needed bug for such complex forms).

I should’ve been more clear, I meant a case where for example a Save-button should enable/disable based on whole binder validity state. I think your case is for when Save is pressed?

Yes, our case comes from an always-on save button. A disabled save Button is against our accessibility policies… and yes; I understand that concern you have with that

The intention is that values are written to the bean only when that would leave the bean in a consistent state, i.e. when all validations pass.

I haven’t tested 24.4-alpha; but once beta is released, I’m going to test it with out applications with some advanced binder usages, but just looking at this I would say it would break our apps because we rely on getBean() to always returns the up-to-date instance… why should I use setBean if the values aren’t written instantly? If I want to have a consistent bean I would have used read/writeBean in the first place.

At least in the past it was like this if setBean is used:

  • only changed fields are checked; so having two required fields; adding a value in the first… and checking in the second field’s validator that getBean().getValue1() returns != null worked and should still work… meaning value1 was written to the bean if it was valid… it does not matter what other fields say; this binding is valid - therefore it should have been written to the bean
  • Binder::withValidator would block the whole write-through; even tho I didn’t like it… but it was so

I would doubt that setDefaultValidatorsEnabled(false) does anything in this case here.