如何使用 Vaadin 上的 withConverter 方法防止 Binder 中的“空”文本
How to prevent “null” text in Binder using withConverter method on Vaadin
如何在使用 Binder
withConverter
方法时防止显示文本 "null"
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);
结果是:
How do the conversion without losing the validation type?
最后,我找到了使用 vaadin API 的解决方案,感谢您的帮助 @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);
如何在使用 Binder
withConverter
方法时防止显示文本 "null"
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);
结果是:
How do the conversion without losing the validation type?
最后,我找到了使用 vaadin API 的解决方案,感谢您的帮助 @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);