vaadin23 ComboBox.setValue failing with conversion error

622 views Asked by At

I'm using vaadin23 to display a list of timezones in a combobox using a binder:


ComboBox<ZoneId> timezoneField = new ComboBox<>();
timezoneField.setItemLabelGenerator(zoneId -> zoneId.getId());
timezoneField.setItems(Timezones.getZones());
binder.forField(timezoneField)
                .bind("timezone");
final var registration = new Registration(member, organisation);
binder.setBean(registration);

The above code seems to work fine until I try to set a default value:

        UI.getCurrent().getPage().retrieveExtendedClientDetails(extendedClientDetails ->
            {
                int timezoneOffest = extendedClientDetails.getRawTimezoneOffset();
                var possibleZones = Conversions.timezonesFromOffset(timezoneOffest);
                if (possibleZones.size() != 0)
                {
                    // this line throws
                    this.timezoneField.setValue((ZoneId) possibleZones.get(0));
                }

            });

possibleZones returns a list of ZoneId but in the debugger they show as ZoneRegion's. I think this is OK as ZoneRegion is a type of ZoneId. I do the cast when calling setValue just in case.

So when I call:

this.timezoneField.setValue((ZoneId) possibleZones.get(0));

The following error is thrown:

 com.vaadin.flow.data.binder.BindingException: An exception has been thrown inside binding logic for the field element [suppress-template-warning='', _inputElementValue='', _clientSideFilter='false', selectedItem='null', invalid='false', pageSize='50', itemValuePath='key', itemIdPath='key', value='1']
    at com.vaadin.flow.data.binder.Binder$BindingImpl.execute(Binder.java:1542) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.Binder$BindingImpl.doConversion(Binder.java:1286) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.Binder$BindingImpl.doValidation(Binder.java:1306) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.Binder$BindingImpl.validate(Binder.java:1247) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.Binder.lambda$doWriteIfValid$3(Binder.java:2245) ~

...

    at com.vaadin.flow.data.binder.Binder.doWriteIfValid(Binder.java:2246) ~[flow-data-23.0.4.jar:23.0.4]

....
com.vaadin.flow.component.internal.AbstractFieldSupport.setValue(AbstractFieldSupport.java:135) ~[flow-server-23.0.4.jar:23.0.4]
    at com.vaadin.flow.component.AbstractField.setValue(AbstractField.java:181) ~[flow-server-23.0.4.jar:23.0.4]
    at com.vaadin.flow.component.combobox.ComboBox.setValue(ComboBox.java:396) ~[vaadin-combo-box-flow-23.0.5.jar:?]
    at dev.onepub.ui.views.noauth.RegistrationView.lambda$4(RegistrationView.java:171) ~[classes/:?]

...

caused by:

Caused by: java.lang.ClassCastException: Cannot cast java.time.ZoneRegion to java.lang.String
    at java.lang.Class.cast(Class.java:3889) ~[?:?]
    at com.vaadin.flow.data.converter.Converter.lambda$from$957be2b0$1(Converter.java:104) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.Result.of(Result.java:90) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.converter.Converter.lambda$from$b652e465$1(Converter.java:104) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.converter.Converter$1.convertToModel(Converter.java:130) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.converter.Converter$2.lambda$convertToModel$6b579330$1(Converter.java:165) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.SimpleResult.flatMap(SimpleResult.java:65) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.ValidationResultWrap.flatMap(ValidationResultWrap.java:67) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.converter.Converter$2.convertToModel(Converter.java:165) ~[flow-data-23.0.4.jar:23.0.4]
    at com.vaadin.flow.data.binder.Binder$BindingImpl.lambda$doConversion$0(Binder.java:1288) ~[flow-data-23.0.4.jar:23.0.4]

I note that the conversion is complaining about converting ZoneRegion to String.

Given the combobox is of type ZoneId and has a ItemLabelGenerator I dont' see why this error can be generated?

1

There are 1 answers

1
Brett Sutton On

So the answer was to create a convertor. It still doesn't make sense to my why setValue needs a convertor to work given that it is using the same type as the combo box is storing.

For the record here is the convertor:


package dev.onepub.fields.converters;

import java.time.ZoneId;

import com.vaadin.flow.data.binder.Result;
import com.vaadin.flow.data.binder.ValueContext;
import com.vaadin.flow.data.converter.Converter;

public class ZoneIdToStringConverter implements Converter<ZoneId, String>
{
    private static final long serialVersionUID = 1L;

    @Override
    public Result<String> convertToModel(ZoneId fieldValue, ValueContext context)
    {

        // Converting to the field type should always succeed,
        // so there is no support for returning an error Result.
        if (fieldValue == null)
        {
            return Result.ok(ZoneId.systemDefault().getId());
        }
        return Result.ok(fieldValue.getId());
    }

    @Override
    public ZoneId convertToPresentation(String zoneId, ValueContext context)
    {
        // Produces a converted value or an error

        // ok is a static helper method that creates a Result
        if (zoneId == null)
            return ZoneId.systemDefault();
        else
            return ZoneId.of(zoneId);

    }

}

and to call it

ComboBox<ZoneId> timezoneField = new ComboBox<>();
timezoneField.setItemLabelGenerator(zoneId -> zoneId.getId());
timezoneField.setItems(Timezones.getZones());
binder.forField(timezoneField)
    .withConverter(new ZoneIdToStringConverter())
    .bind("timezone");
final var registration = new Registration(member, organisation);
binder.setBean(registration);