Issue
I have a spring boot application with Vaadin and I just need simple UI element validation without backing beans.For example to just check if a textfield value is null,empty or not.
Since validators require a bean I was thinking of using Junit Assert for this simple job. So using Junit not just for running tests but for the main flow of the application.
Is this good practice and what alternatives are there?
Solution
For such use cases where you need to bind Vaadin's Converters or Validators to a single field / value pair without a Bean being involved I have written FieldBinder utility class, which can be found in Vaadin's Directory and added as maven dependency to your project.
https://vaadin.com/directory/component/fieldbinder
// Binder with integer
FieldBinder<Integer> integerFieldBinder = new FieldBinder<>();
TextField integerField = new TextField("Input number");
// Text field with Integer value Converter and Validator
// Demoing how to detect if value is valid and how to get it from FieldBinding
FieldBinding<Integer> integerBinding = integerFieldBinder.forField(integerField)
.withConverter(new StringToIntegerConverter("This is not a number"))
.withValidator(new IntegerRangeValidator("Give a number between 5 and 10",5,10))
.bind(integerValue);
Answered By - Tatu Lund
Answer Checked By - Pedro (JavaFixing Volunteer)