BeanValidationBinder not working?

I created a BeanValidation binder
import com.vaadin.flow.data.binder.BeanValidationBinder;
private Binder binder = new BeanValidationBinder<>(NotizEntity.class);

added some binds

binder.bind(titel, NotizEntity::getTitel,
NotizEntity::setTitel);
binder.bind(notizen, NotizEntity::getNotizen,
NotizEntity::setNotizen);

The NotizEntity has:

import javax.persistence.Entity;
import javax.validation.constraints.NotEmpty;

@Entity
public class NotizEntity extends AbstractEntity {

@NotEmpty
private String titel;

private String notizen;

Unfortunately on binder.write there seems to be no validation happening. binder.validate says validation is Ok.

I checked:
Optional<Binding<NotizEntity, ?>> binding = binder.getBinding(“titel”);
→ here the binding is empty.

getting and setting the bean works fine in the form I created, but the validation for the NotEmpty annotation is not happening. Any ideas?

You should bind by property name

With BeanValidation I also suggest to use “non-buffered mode”. You’ll appreciate that decision on the day you want to drop in a class level validator (aka cross field validation).

In other words, use setBean instead of writeBean/readBean

Hi, so I have to define the constraint myself? ‘binder.forField(yearOfBirthField)
.withValidator(
yearOfBirth → yearOfBirth <= 2000,
“Year of Birth must be less than or equal to 2000”)
.bind(Person::getYearOfBirth, Person::setYearOfBirth);’

I thought the BeanValidationBinder would read the annotation itself?! Or am I missing something here?

bind(“yearOfBirth”) instead of functional references (Class::method)

Ah yes that works, in my case: binder.bind(titel, “titel”);
binder.bind(notizen, “notizen”);

thank you :slightly_smiling_face: