FormBinder - binding object

There is a very good question in the addons rating:

This is exactly my problem, a select component (drop down) with a container datasrc… It would be great to bind this thing.

Class: Product Bean
-String name
-Vendor vendor

Class: Vendor Bean
-String vendorname

By the way, a very good addon!

I didn’t get exactly what you are trying to do, but there is an API added for that question. I just haven’t had a change to modify the answer due to a Directory limitation.

This is the API that might help you too:

http://dev.vaadin.com/browser/svn/incubator/PreCreatedFieldsHelper/src/org/vaadin/addon/formbinder/ViewBoundForm.java#L88

cheers,
matti

Sorry my fault.

This addon is so **** great!

It’s very easy to map pojos inside pojos :slight_smile:

Product form layout with drop down with Vendor pojos inside:


...
BeanItemContainer<Vendor> container = new BeanItemContainer<Vendor>(Vendor.class);
...
vendorField = new Select("",container);	
vendorField.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
vendorField.setItemCaptionPropertyId("name");
vendorField.setImmediate(true);
...

Create the form and bind it:


...
product = new Product();
form = new ViewBoundForm(new ProductFormView());
form.setImmediate(true);
form.setItemDataSource(new BeanItem<Product>(product));
formLayout.addComponent(form);
...

Ok creating data this way is working also with nested attributs form the dropdown.

But getting nested attributes (like Product.vendor) field up with existing data, seems to be a problem…

Hi,

I’m probably already bit on holiday mood, but I didn’t really get what you are trying to accomplish. If you can elaborate you problem a bit I could think if there is something I could improve in FormBinder (or even in Vaadin).

cheers,
matti

Ok, I have two beans Product–> Vendor

Product.class



public class Product implements Serializable{
	
        .....

	private Vendor vendor;

	public Vendor getVendor() {
		return vendor;
	}

	public void setVendor(Vendor vendor) {
		this.vendor = vendor;
	}
        ....
}

Vendor.class


public class Vendor implements Serializable{
        .....
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
        .....
}

The Form Layout for a Product including a dorp down for its Vendor


public class ProductFormView extends CustomComponent {
	
	...
	
	public ProductFormView() {
		buildMainLayout();
		setCompositionRoot(mainLayout);

		// TODO add user code here
		initVendors();
	}
	
	private void initVendors(){
		vendorPaginator = new BeanLocator.GlobalJNDIName()
		.withBusinessInterface(VendorPaginatorRemote.class)
		.withBeanName(VendorPaginator.class);
		
		BeanItemContainer<Vendor> container =new BeanItemContainer<Vendor>(Vendor.class);
				
		while(vendorPaginator.hasNext()){
			for(Vendor curVendor : vendorPaginator.next()){
				container.addBean(curVendor);
			}
		}
		vendorField = new Select("Vendors",container);	
		vendorField.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
		vendorField.setItemCaptionPropertyId("name");
		vendorField.setImmediate(true);
		vendorField.setInvalidAllowed(true);
		vendorField.setNewItemsAllowed(true);
		
		mainLayout.addComponent(vendorField);
		
		vendorField.addListener(new Property.ValueChangeListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				getWindow().showNotification("bla"+ ((Vendor)vendorField.getValue()));
			}
		});
	}

	....
}

Using the form


                ....
		Form productForm = new ViewBoundForm(new ProductFormView());
		formLayout.addComponent(productForm);
                productForm.setItemDataSource(new BeanItem<Product>(fetchedProduct));
                ....

The Form writes all Product attributes including the vendor. I’m also able to load the form with an existing Product, but the Vendor field is empty…

Hi,

So your get that select on your UI fine, but its value is not reflected to expected one? IMO your code looks just fine and it should work.

One thing that comes to my mind is that maybe you have different instances of Vendor objects in the Product object and when listed via your data access layer. Vaadin select matches objects with Object.equals() method, not by just string comparison. So I’d suggest to check this with debugger (e.g. put breakpoint to AbstractSelect.setValue() method), and then implement equals method properly in you data objects if necessary.

cheers,
matti

You made my day! Just implement a real queals method and its working now. Big thx!

Another problem.
How to bind a BigDecimal propertyto an textfield?

Hi,

I have never done that myself. You could add a new forum topic about it so others could also find the solution that comes up. I think I’d start with PropertyFormatter (or PropertyTranslator add-on).

cheers,
matti

Thx for this advice. Will try it.

By the way, is there a way to bind a table from the form to the beanitemdatasource from the form with FormBinder?