Binding one property to multiple fields with FormBinder

I’ve been trying to bind a single property to multiple fields with FormBinder. Below is small example of what I’ve been trying to do:


import org.vaadin.addon.formbinder.FormFieldMatch;
import org.vaadin.addon.formbinder.FormView;
import org.vaadin.addon.formbinder.PropertyId;
import org.vaadin.addon.formbinder.ViewBoundForm;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.data.util.PropertysetItem;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class MultiPropertyBinding extends Window {

  private static final String PROPERTY = "property";

  private static final String PROPERTY_COPY = "property_copy";

  public MultiPropertyBinding() {
    TheForm form = new TheForm();

    PropertysetItem item = new PropertysetItem();
    ObjectProperty<String> property = new ObjectProperty<String>("Initial value");
    item.addItemProperty(PROPERTY, property);
    item.addItemProperty(PROPERTY_COPY, property);

    ViewBoundForm boundForm = new ViewBoundForm(form);
    boundForm.setItemDataSource(item);
    addComponent(boundForm);
    setWidth("200px");
  }

  @FormView(matchFieldsBy = FormFieldMatch.ANNOTATION)
  private static class TheForm extends FormLayout {

    @PropertyId(PROPERTY)
    private final TextField field1 = new TextField("Field 1");

    @PropertyId(PROPERTY)
    private final TextField field2 = new TextField("Field 2");

    @PropertyId(PROPERTY_COPY)
    private final TextField field3 = new TextField("Field 3");

    public TheForm() {
      field1.setWidth("150px");
      field2.setWidth("150px");
      field3.setWidth("150px");

      addComponent(field1);
      addComponent(field2);
      addComponent(field3);
    }
  }
}

So the form displays 3 text fields (numbered 1 to 3), with 1 and 2 being bound to the exact same property using the same property id and number 3 using the same property, but a different property id. When the form is displayed, I was expecting all three fields to display “Initial value” because they are all referring to the same property. However, when I run it, I’m observing that only the first and third text fields are displaying it (the second field is blank).

Is there something that I am missing or does the form binder only support binding each property id to only one field?

Im having the same problem! Any suggestion ?

Hi,

I don’t exactly remember how the binding works, but I’m quite sure the logic just loops fields until it finds a matching one and then moves on to next property. So the second field never gets a data source.

If you wish to change that, I’d suggest to make there a mode that supports binding property to several fields. I’ll be happy to commit and publish a new version with such changes.

cheers,
matti

We ended up working around it by giving the property an alias and binding the second field to the alias.

Can you give an example on how you did that ?

The code in the first post of this thread actually contains an example of the work around (see how field1 and field3 are bound to PROPERTY and PROPERTY_COPY).

I’ve added a ticket for this: http://dev.vaadin.com/ticket/12932