How to map empty Vaadin TextField to null value in model property

141 views Asked by At

Default behavior of Vaadin v24 is to map empty TextField to an empty String as model value. How to map it to null value for all TextFields without adding withNullRepresentation to every textField binding?

I've made my own CustomBinder and I'm wondering is this the right way to go. This is the code:

public class CustomBinder<BEAN> extends Binder<BEAN> {
    private static final long serialVersionUID = 3555096816850091643L;

    public CustomBinder() {
        super();
    }

    public CustomBinder(Class<BEAN> p_beanType, boolean p_scanNestedDefinitions) {
        super(p_beanType, p_scanNestedDefinitions);
    }

    public CustomBinder(PropertySet<BEAN> p_propertySet) {
        super(p_propertySet);
    }

    public CustomBinder(Class<BEAN> p_class) {
        super(p_class);
    }

    @Override
    public <FIELDVALUE> BindingBuilder<BEAN, FIELDVALUE> forField(HasValue<?, FIELDVALUE> p_field) {
        BindingBuilder<BEAN, FIELDVALUE> forFieldBinding = super.forField(p_field);
        if (p_field instanceof TextField || p_field instanceof TextArea) {
            forFieldBinding = forFieldBinding.withNullRepresentation((FIELDVALUE) "");
        }
        return forFieldBinding;
    }
}
0

There are 0 answers