If domain class contain getter with Optional type of returned value:
public Optional<String> getName() {
return Optional.ofNullable(name);
}
And use BeanValidationBinder :
BeanValidationBinder<Employee> binder = new BeanValidationBinder<>(Employee.class);
binder.forField(name).withValidationStatusHandler(new BindingValidationStatusHandler() {
private static final long serialVersionUID = 1L;
@Override
public void statusChange(BindingValidationStatus<?> statusChange) {
if (statusChange.getMessage().isPresent()) {
name.setComponentError(new UserError(statusChange.getMessage().get()));
} else {
name.setComponentError(null);
}
}
})
.bind("name");
After data input in field appear error:
…
Caused by: java.lang.ClassCastException: Cannot cast java.lang.String to java.util.Optional
…
Source code to reproduce error:
https://github.com/zesetup/vaadin8binder.git