Form and Nested Properties

Hello,

I am facing a beginner’s issue and hope to obtain some help here. :slight_smile:

Well, firstly, what I am trying to do is a form that contains native fields (String) and a reference to another entity (User).

My model class:

class Document {
String name;
User admin;
}

I am using JPAContainer, for the table and it lists everything perfectly.

Now, I need to represent the admin property as a combobox, in the form and it will list all my registered users, so I can select one.

Now, how can I achieve that? Is there a known best-practice for this kind of situation?

Thanks a lot!

Marcos Alcantara

Hi,

I believe something like this will do it:

form.setFormFieldFactory(new DefaultFieldFactory() {
    public Field createField(Item item, Object propertyId, Component uiContext) {
        if (propertyId.equals("user")) {
            ComboBox adminsCombo = new ComboBox("Admin", collectionOfAdminUsers);
            return adminsCombo;
        }
        return super.createField(item, propertyId, uiContext);
    }
);

Just get you collection of admin users from a helper class or any other way you see fit.

Cheers.

You should also be able to create the combo box using a JPAContainer of administrators. If you are using caching in JPAContainer, this should not be very costly, and makes sure your list is up to date.

You should not share an instance of JPAContainer between different combo boxes in different contexts (as that could lead to memory leaks) but simply create a new instance.

I believe there is an example of something like that in the JPAContainer sample application.