Create FormField programmatically

Hi,

I love how FormFields automatically select the good Field type for different property types (string, dates, enums, etc…). Is this automatic selection possible when creating a Form Field programmically?
I have tried uiComponents.create(Field.class) but this does not work.

Thanks

Hi,

The Form component loader uses the UiComponentsGenerator bean under the hood, so you can do the same, e.g.:

@Inject
private Metadata metadata;
@Inject
private UiComponentsGenerator uiComponentsGenerator;

@Subscribe
public void onInit(InitEvent event) {
    MetaClass metaClass = metadata.getClassNN(User.class);
    ComponentGenerationContext context =
            new ComponentGenerationContext(metaClass, "group");
    Component component = uiComponentsGenerator.generate(context);
}

Regards,
Gleb

2 Likes

Exactly what I was looking for!
Many thanks,

Matthis

1 Like