Possible bug using form component together with fragment

Hello everyone, I’m trying to add a fragment inside a form component. The form looks good but has a component alignment error which I don’t know if it’s its default behavior or if it’s a bug.

image

I use the following code in the xml of the screen

<groupBox id="overviewBox" caption="mainMsg://overviewBox.caption">
            <form id="form" dataContainer="taxAuthorityDc">
                <column width="770px">
                    <textField id="identificationField" property="identification" width="180px"/>
                    <textField id="nameField" property="name"/>
                    <fragment screen="trz_RefersToAddressFragment">
                        <properties>
                            <property name="addressDc" ref="addressDc"/>
                            <property name="addressTypeField" value="false"/>
                            <property name="regionField" value="false"/>
                            <property name="zoneField" value="false"/>
                            <property name="latitudeField" value="false"/>
                            <property name="longitudeField" value="false"/>
                            <property name="hiddenField" value="false"/>
                        </properties>
                    </fragment>
                    <fragment screen="trz_RefersToContactFragment">
                        <properties>
                            <property name="contactDc" ref="contactDc"/>
                        </properties>
                    </fragment>
                    <checkBox id="hiddenField" property="hidden"/>
                </column>
            </form>
        </groupBox>

Whether or not it is your default behavior how could you fix it?

Regards,

Nelson F.

Hi,

It’s not a bug, but expected behavior. The Form component manages captions of directly nested components. In your case, a fragment such nested component which can have its own caption and Form reserved a place for it. You’ll get the same result if you place a container with some fields or another Form (which is actually a container itself).

It seems that your fragment contains only fields to represent the Address entity, so I’d recommend instead of using fragments implement a bean (or helper class) that adds required fields to a passed Form component depending on some criteria.

Regards,
Gleb

Hello @gorelov , thank you understood. What is not clear to me is

Could you help me with an example?

Regards,

Nelson F.

Let’s assume that I have the following bean definition:

@Component(ComponentsHelper.NAME)
public class ComponentsHelper {
    public static final String NAME = "demo_ComponentsHelper";

    @Inject
    protected UiComponents uiComponents;
    @Inject
    protected MessageTools messageTools;

    public void setupForm(Form form, InstanceContainer<Product> productDc) {
        TextField<BigDecimal> field = uiComponents.create(TextField.TYPE_BIGDECIMAL);
        field.setValueSource(new ContainerValueSource<>(productDc, "price"));
        field.setCaption(messageTools.getPropertyCaption(productDc.getEntityMetaClass(), "price"));
        field.setSizeFull();

        form.add(field);
    }
}

I can use it to add fields to a form component:

public class ProductEdit extends StandardEditor<Product> {

    @Inject
    protected Form form;

    @Inject
    protected InstanceContainer<Product> productDc;

    @Inject
    protected ComponentsHelper componentsHelper;

    @Subscribe
    protected void onInit(InitEvent event) {
        componentsHelper.setupForm(form, productDc);
    }
}