Required TextField validation with Binder

I have a required TextField and set it up like this:

TextField title = new TextField("Title");
title.setRequired(true);

The validation works as expected. An empty field will get invalid state and turns red.

As soon as I use a Binder for this, the validation is not visible to the user anymore:

Binder<Dto> binder = new Binder<>(Dto.class);
binder.bindInstanceFields(this);
binder.setBean(dto);

Leaving the field, the validator runs and calls the com.vaadin.flow.component.shared.HasValidationProperties#setInvalid(true).

But then the binder kicks in and overwrites to false here com.vaadin.flow.data.binder.Binder#clearError.

Although, the field is empty, when calling binder.validate(); it does not show any errors.

What is the intention of the binder overwriting the validation?
How can I have the default behaviour of field turning red on empty input?
Why is binder.validate().isOk() == true although my field is empty?

Thank you.

References:

Oh no. Sorry to answer my own question. Checked the wrong documentation.

.forMemberField should be used:

binder.forMemberField(title).asRequired();
binder.bindInstanceFields(this);
binder.setBean(dto);

Reference:

1 Like

Good that you found the solution! The background is indeed that when you bind a field with Binder, then Binder takes over validation of that field and this overrides any validation configuration applied directly for the field. The proper way is thus to configure the field to be required through Binder instead.

1 Like