No announcement yet.
  • Filter
  • Time
Clear All
new posts

    Best practice: Changing DynamicForm fields

    Hi Isomorphic,

    can you tell what's the best practice when one has to change a field of a DynamicForm, but the fields property is "IR", but not "IRW".

    I have the case where I want to upload an attachment. The attachment can have a type (coming from a SelectItem) like "Contract" or "Photo".
    Depending on the value here I want to set FileItem.accept, which is "IR", so I need to update it.

    Now, can/should I call DynamicForm.setFields with all the old fields via DynamicForm.getFields + a new instance of FileItem with correct accept, or what is the suggested way of doing this?
    There seems to be no method setItem(fieldName, FormItem), that could be used to replace the item without a "W".

    Thank you & Best regards

    Right now there's no way to replace an individual item without a form rebuild, but you could break out the FileItem into a separate form so that you can just rebuild that one part - you'd link them together via a ValuesManager.


      Hi Isomorphic,

      yes the ValuesManager idea is also a good one, thanks.

      W.r.t other possibilities, as more general question: When you say "without a form rebuild", do you mean:
      1. a new DynamicForm() with all new FormItem()
      2. a setFields() call with new FormItems
      3. a setFields() call with a lot reused FormItems from the existing form via getFields() and the changed new FormItem()

      Is there any solution better / worse than another? I know that you can use a FormItem instance in only one form, but are you allowed to "recycle" them?

      Thank you & Best regards


        You can create a new DynamicForm or call setFields(), either way, you need entirely new FormItems.

        Since the values are still preserved if you call setFields(), this is pretty simple and doesn’t require a lot of new code, since if you have a function that builds the FormItems in the first place, you just run that function again and call setFields().

        If the form has dynamic state created as the user interacted with it (like you have enabled or disabled fields, or shown or hidden them), if those dynamic states are represented by enabledWhen and similar properties, then again there’s no work to preserve such state, since the same state will be in place when the items are all replaced.

        However it is more work (in terms of the DOM and generating new components, not in terms of code) to completely rebuild the form, so that’s why, if you have a part of the form where you need to rebuild items, we recommended just making that part a separate form. Full rebuild would be instant for a small form but there might be a slight delay for a large form. Also, a partial rebuild means you don’t even have to think about whether there is dynamic state that you may need to preserve.


          Hi Isomorphic

          thanks for the detailed explanation. That all makes sense. I'm not sure where to put it, but think that this information ("do not reuse fields, either create a new form or call setFields() with a method that produces new instances") might be helpful in the docs.
          Perhaps one of these:Best regards