Hi all!
In Vaadin 7 version of our project we were making extend use of the getConvertedValue method of the AbstractField, which is now missing and I wanted to know if there is any better workaround that the one I have thought of.
Actually I need to validate a TextField which binds to a Double property of my bean, against another TextField which binds to another Double property. The two fields are called amountFromField and amountToField, and I need to validate that the number entered in the first field is no greater than the number in the second field (there is similar example about cross-field validation in
Binding Data to Forms
documentation but there is no conversion involved in the specific example).
During validation of the amountFromField I new to convert the input of the amountToField to a double, in order to make the comparison. As demostrated below, I create a new StringToDoubleConverter object, and use the convertToModel method to get the double value of the TextField, which ends up to some quite cumbersome code.
[code]
Binding<FilterBean, Double> amountFromBinder =
binder.forField(amountFromField)
.withConverter(new StringToDoubleConverter(EnglishStatics.MSG_NUMERIC_VALUE))
.withValidator(amountFrom β {
StringToDoubleConverter sdc = new StringToDoubleConverter(EnglishStatics.MSG_NUMERIC_VALUE);
Double amountTo = sdc.convertToModel(amountToField.getValue(), new ValueContext(amountToField.getLocale())).getOrThrow(msg β new IllegalArgumentException(msg));
return amountFrom.compareTo(amountTo) <= 0;
},
EnglishStatics.CAPTION_FROM_VALUE_AFTER_TO_VALUE)
.bind(FilterBean::getAmountFrom, FilterBean::setAmountFrom);
binder.forField(amountToField)
.withConverter(new StringToDoubleConverter(EnglishStatics.MSG_NUMERIC_VALUE))
.bind(FilterBean::getAmountTo, FilterBean::setAmountTo);
// Revalidate amountFromField when amountToField changes
amountToField.addValueChangeListener(event β amountFromBinder.validate());
[/code]Is this the correct way to get the converted value from a field?
I have not even run the code to see that it does what I need, I am in the middle of migrating just a small part of out project to pure Vaadin 8 and decide if it worths the effort, so please be forgiving if itβs all wrong or are unhandled exceptions in there!