AnsweredAssumed Answered

Add field to form dynamically

Question asked by stan on Jun 6, 2016
Latest reply on Jan 3, 2017 by kranthi
I need to store multiple contact persons related to some object. Persons are external to the system and won't be added as users/members.
So I've created an association with custom contact type in the model:

        <aspect name="co:objectInfo">
                <child-association name="co:contacts">

        <type name="co:contact">
            <title>Contact Person</title>
                <property name="co:person">
                <property name="co:phone">

When I create the object, I'd like to populate contacts data in create form. It should look like multiple person/phone text field pairs in UI.
My idea is to use Form Filters.

If we're talking about edit form, then it seems it is possible to implement this.
In beforeGenerate method we can fetch associations from NodeRef, dynamically create fields, populate them and add to form.
In beforePersist we can parse these fields (identifying them by some name prefix or postfix) and update associations.

But how can I add(/delete) an empty fields pair? I can provide some Add (and Delete) button in Freemaker template for my custom control, add JavaScript.
But I have not found any documentation or examples how can I do it using JavaScript.

It seems we have FormUI.options.fields property, but I'm not sure how to use it. Also I doubt if we can change form model after generation from client side.

Any information or examples how we can update form using client side JavaScript are highly appreciated.
Alternatively what are best practices if we need to manage multiple simple associations? It seems it is rather typical case.

Thanks in advance for any thoughts.