我有一个带有组合框的输入表单,它显示整数值。我希望用户能够向此组合框添加新的整数值。我需要验证输入是否只是数字(而不是字母) - 如果它不是一个有效的整数,我想显示一条错误消息。
对于文本字段,我很快就明白了——即使输入“错误”输入,我也会收到一条错误消息!但是我找不到与 addCustomValueSetListener 结合使用的组合框的解决方案。
我正在使用 Vaadin 14.1.21 和 Java JDK+JRE 1.8。
现在,如果我输入带有字母的自定义值,我不会在框下方显示错误消息,并且当我想“保存”它/将其存储在数据库中时,它只是默默地忽略输入值。
public class MyForm extends Div {
private TextField tf;
tf =new TextField("TF");
tf.setWidth("100%");
tf.setRequired(true);
tf.addThemeVariants(TextFieldVariant.LUMO_ALIGN_RIGHT);
tf.setValueChangeMode(ValueChangeMode.EAGER);
private ComboBox<Integer> combo_int;
combo_int= new ComboBox<>();
combo_int.setItems(114, 12383, 65432189);
combo_int.setLabel("Some ID");
combo_int.addCustomValueSetListener(
event -> combo_int.setValue(Integer.parseInt(event.getDetail()))
// since I need to parse new values here, I cannot use a validator upon binding
);
binder = new BeanValidationBinder<>(MyData.class);
binder.forField(tf)
.withNullRepresentation("")
.withConverter(new StringToIntegerConverter("needs to be integer!"))
.bind("tf_data_integer");
binder.forField(combo_int)
.bind("integer_data");
}