Forms with Nested Beans

I have a form, following the book, setting a bean as data source. Everything works fine with the exception of the field referencing a nested bean. For simplification purposes let’s use this two beans:

public class Requirement implements Serializable {

    private String uniqueId;
    private String description;
    private String notes;
    private RequirementType requirementTypeId;
    <getters and setters>
}

And the nested bean:

public class RequirementType implements Serializable {

   private String name;
   <getters and setters>
}

When I feed a completely populated bean to the form the requirementTypeId gets displayed as the string representation of the RequirementType but I would want the name value of the bean displayed instead.

I tried to refer to the field as requirementTypeId.name, following the
example
done on a table but it doesn’t seem to work.

In case it helps, here’s the code to generate the form:

// Create a form and use FormLayout as its layout.
        final Form form = new Form();
        // Set form caption and description texts
        form.setCaption(edit ? "" : getInstance().getResource().getString("message.requirement.create"));
        // Set the form to act immediately on user input. This is
        // necessary for the validation of the fields to occur immediately
        // when the input focus changes and not just on commit.
        form.setImmediate(true);
        //Set field factory to create the fields of the form
        form.setFormFieldFactory(new FormFieldFactory() {
            @Override
            public Field createField(Item item, Object propertyId, Component uiContext) {
                // Identify the fields by their Property ID.
                String pid = (String) propertyId;
                if ("uniqueId".equals(pid)) {
                    TextField textField = new TextField(getInstance().getResource().getString("general.unique.id") + ":");
                    if (!edit) {
                        textField.addValidator(new Validator() {
                            @Override
                            public void validate(Object value) throws InvalidValueException {
                                if (!isValid(value)) {
                                    throw new InvalidValueException("Invalid value: " + value);
                                }
                            }

                            @Override
                            public boolean isValid(Object value) {
                                HashMap<String, Object> parameters = new HashMap<String, Object>();
                                parameters.put("id", value);
                                parameters.put("product",
                                        new ProductJpaController(
                                        DataBaseManager.getEntityManagerFactory()).findProduct(currentProduct.getId()));
                                List<Object> result = DataBaseManager.createdQuery(
                                        "select r from Requirement r where "
                                        + "r.uniqueId=:id and r.productId=:product", parameters);
                                if (result.isEmpty()) {
                                    return true;
                                }
                                return false;
                            }
                        });
                    }
                    textField.setEnabled(!edit);
                    return textField;
                }
                if ("description".equals(pid)) {
                    TextArea textArea = new TextArea(getInstance().getResource().getString("general.description") + ":");
                    textArea.setEnabled(true);
                    return textArea;
                }
                if ("notes".equals(pid)) {
                    TextArea textArea = new TextArea(getInstance().getResource().getString("general.notes") + ":");
                    textArea.setEnabled(true);
                    return textArea;
                }
                if ("requirementTypeId".equals(pid)) {
                    Select select = new Select(
                            getInstance().getResource().getString("message.requirement.type") + ":");
                    for (Iterator<Object> it = DataBaseManager.namedQuery("RequirementType.findAll").iterator(); it.hasNext();) {
                        RequirementType rt = (RequirementType) it.next();
                        select.addItem(rt.getName());
                    }
                    //Allow to create new ones
                    select.setNewItemsAllowed(true);
                    if (!edit) {
                        select.addValidator(new Validator() {
                            @Override
                            public void validate(Object value) throws InvalidValueException {
                                if (!isValid(value)) {
                                    throw new InvalidValueException("Invalid value: " + value);
                                }
                            }

                            @Override
                            public boolean isValid(Object value) {
                                return value != null && !value.toString().trim().isEmpty();
                            }
                        });
                    }
                    if (edit) {
                        LOG.info(r.getRequirementTypeId().getName());
                        select.setValue(r.getRequirementTypeId().getName());
                    }
                    return select;
                }
                return null;//Invalid field
            }
        });
        // Create a bean item that is bound to the bean.
        BeanItem item = new BeanItem(r);
        // Bind the bean item as the data source for the form.
        form.setItemDataSource(item);
        //Set the order of fields to display as well as which fields to hide
        ArrayList<String> fields = new ArrayList<String>();
        //Required fields
        fields.add("uniqueId");
        fields.add("description");
        fields.add("requirementTypeId");
        for (String field : fields) {
            form.getField(field).setRequired(true);
            form.getField(field).setRequiredError(
                    getInstance().getResource().getString(
                    "message.required.field.missing").replaceAll("%f", field));
        }
        //Non required fields
        fields.add("notes");
        form.setVisibleItemProperties(fields);

Any hints?

Looks like this was already answered
here
. I’ll give it a try and post back the results.

It worked! :grin: