Search code examples
javavaadinvaadin8

How to prevent “null” text in Binder using withConverter method on Vaadin


How to prevent show text "null" when using Binder withConverter method

    TextField id = new TextField("Id");
    TextField name = new TextField("Name");

        Binder<Customer> b = new Binder<>();

        b.forField(id)
                .withConverter(Integer::valueOf, String::valueOf, "Invalid")
                .bind(Customer::getId, Customer::setId);
        b.forField(name)
                .bind(Customer::getName, Customer::setName);
        b.readBean(customer);

And the result is it:

enter image description here

How do the conversion without losing the validation type?


Solution

  • Finally, I found a solution using the vaadin API, thanks for the help @Shirkam

        b.forField(id)
                .withConverter(Integer::valueOf, String::valueOf, "Invalid")
                .withNullRepresentation(0)
                .withValidator(new IntegerRangeValidator("Value must be greater than 0 ", 1, null))
                .bind(Customer::getId, Customer::setId);
        b.forField(name)
                .bind(Customer::getName, Customer::setName);
        b.readBean(customer);