Reputation: 2913
I am using Vaadin 14.1.21 and want a Dropdown box (Combobox) to display Integer-values, and also allow custom input.
I have Integer in the dataclass for the field as a type, but the UI fieldtype is String since I also need to enable custom input, . I use withConverter in combination with a custom converter as I don't want to display the thousand-separator (dot in my case) in the UI. It works as expected for all supplied drop down items - and when I select the supplied 'invalid' item of the dropdown menu ("Error Message Item"), I get my custom error displayed beneath the combobox.
The problem appears when I input an invalid custom value, e.g. "329hhh2" - I get no error upon invalid custom-input from my custom converter!
I don't get any error at all and when I want to "save" my object, the (invalid) combobox value just disappears into nirvana and I get Null as the field-value.
Can someone tell me what I'm doing wrong?
Here is my code example (i tried to keep it as short as possible without leaving anything out):
public class RisikoForm extends Div {
private ComboBox<String> number_string_combobox;
public void instantiate_string_combobox() {
number_string_combobox = new ComboBox<>();
number_string_combobox.setItems("6583212", "114514", "879278", "Error Message Item");
number_string_combobox.setLabel("Number");
number_string_combobox.addCustomValueSetListener(event -> event.getDetail());
}
private static class Id_StringToInteger_Converter extends StringToIntegerConverter {
public Id_StringToInteger_Converter() {
super("Custom Error: Input cannot be converted to integer");
}
@Override
protected NumberFormat getFormat(Locale locale) {
final NumberFormat format = super.getFormat(locale);
format.setGroupingUsed(false);
return format;
}
}
public Form(ViewLogic sampleCrudLogic) {
this.instantiate_string_combobox();
content.add(number_string_combobox);
binder = new BeanValidationBinder<>(Data.class);
binder.forField(number_string_combobox)
.withNullRepresentation("")
.withConverter(new Id_StringToInteger_Converter())
.bind(Data::getIntegerNumber, Data::setIntegerNumber);
}
}
public class Data implements Serializable {
private Integer integerNumber;
public Integer getIntegerNumber() {
return integerNumber;
}
public void setIntegerNumber(Integer integerNumber) {
this.integerNumber = integerNumber;
}
}
Upvotes: 2
Views: 738
Reputation: 37008
Your custom value set listener is a no-op right now. You have to react on the event. E.g.:
addCustomValueSetListener(event -> number_string_combobox.setValue(event.getDetail()))
See the example under "Allow custom values" or "Storing custom values"
Upvotes: 3