FormBinder - Select not getting value from bean

(This is similar “FormBinder - binding object” thread from last December, except the solution there doesn’t apply because I’m just dealing with Strings, which have an equal() implementation.)

I have a field for time zone, which is just the String ID value from java.util.TimeZone. The Select box displays fine, with values to select, but the existing value isn’t pulled from the bean. I confirmed in the debugger that the “value” member variable in the Select object is null. The two TextFields fill in from the bean just fine.


public class LocationView extends CustomComponent
{
  @AutoGenerated
  private VerticalLayout mainLayout;
  @AutoGenerated
  private Select timezoneField;
  @AutoGenerated
  private HorizontalLayout horizontalLayout;
  @AutoGenerated
  private TextField shortNameField;
  @AutoGenerated
  private TextField nameField;

  public LocationView()
  {
    buildMainLayout();
    setCompositionRoot(mainLayout);

    // Fill in Time Zone selection field
    String[] zones = TimeZone.getAvailableIDs();
    Arrays.sort(zones);
    for (String id : zones)
      if (id.startsWith("US/") || id.startsWith("Canada/"))
        timezoneField.addItem(id);
    timezoneField.setScrollToSelectedItem(true);
  }
...

Hi,

Is your property type TimeZone or String? Selects identifier type and the type of you property must match. This is not just with FormBinder but with all Vaadin bean binding.

If you need to do conversions between selects identifiers and the value of the property, your could add
PropertyTranslator
to you Select.

cheers,
matti

The items are Strings, as shown above, as is the data type in the bean.


  /**
   * Get the timezone name.
   * @return the timezone
   */
  public String getTimezone()
  {
    return timezone;
  }

  /**
   * @param timezone the timezone to set
   */
  public void setTimezone(String timezone)
  {
    this.timezone = timezone;
  }

I noticed that another Select component with only two items is working fine. The Time Zone list has 22 options in it, and Select appears to be lazy-loading nine at a time. If the value from the bean is one of the first nine, then FormBinder will select it. If it’s later in the list though, it doesn’t.

I wanted to be sure this was a FormBinder problem and not a gaping widget bug, so I added:

timezoneField.setValue("US/Central");

to the end of the LocationView constructor and didn’t set the item data source with the bean. That worked, despite the US/Central value being on the second page of the Select listing. When I initialize from the bean again, FormBinder unselects this.

Ah, now I got it. So you are setting the value of the field in you view class?

I’m afraid that is rather tricky as the Form sets the property of your item for the field after your view class constructor has been called. Instead you should just ensure that your property has the correct initial value (or e.g. set the value straight to field after data sources has been set if you are using buffering).

cheers,
matti

No, you aren’t getting it. Setting the value directly was just an experiment to confirm that it is a FormBinder bug I am dealing with.

The bean object has the value, but when bound to the view the value is not pre-selected in the Select widget if the value is not on the first nine options in the list. Select paginates the options nine at a time. (I haven’t found how to adjust this.)

The test confirms that Vaadin’s Select.setValue() works even though the option beyond the first page of options. FormBinder does not.

Never mind. I walked through the Form and Property classes in the debugger a few times and found my bug. :-/
Sorry to bother you.