Hello everyone,
I have problems using a converter for a binding.
Usecase: I got some Numberfields. Now I want to check whether the user entered letters instead of numbers so that my binding doesn’t cause an error.
This is the binder I wrote: (based on the StringToInteger converter in the vaadin documentation)
public class StringToDoubleConverter implements Converter<String,Double> {
@Override
public Result<Double> convertToModel(String fieldValue, ValueContext context){
// produces a converted value or an error
try {
// ok is a static helper method that creates a Result
return Result.ok(Double.valueOf(fieldValue));
} catch (NumberFormatException nfe) {
// error is a static helper method that creates a result
return Result.error("Bitte geben Sie eine gültige Zahl ein.");
}
}
@Override
public String convertToPresentation(Double doubleValue, ValueContext context) {
// Converting to the field type should always succeed,
// so there is no support for returning an error Result.
return String.valueOf(doubleValue);
}
}
This is where I use the binder:
binder.setBean(entry);
binder.bindInstanceFields(this);
binder.forField(dbPulse).withStatusLabel(statusLabelPulse).withNullRepresentation(0d);
StringToDoubleConverter convStrToDblConverter = new StringToDoubleConverter();
binder.forField(dbPulse).withConverter(convStrToDblConverter);
This is the error I get and I don’t understand it because it says that my parameter for the withConverter method has the wrong type, which is just not true:
The method withConverter(Converter<Double,NEWTARGET>) in the type Binder.BindingBuilder<MedicalJournalEntry,Double> is not applicable for the arguments (StringToDoubleConverter)
Thanks for any help in advance!