Form with variable number of fields


Quick question: I’m looking to create a form with a variable number of fields, so the user can enter more fields depending on what they want to post. It’s essentially a search query builder where I would like to have one of more ‘conditions’ displayed along with a ‘+’ to add more fields. I can’t seem to find a way to produce this with Forms as is, seems as if the structure should be fixed from the start, even with CustomField. Anyone tried this and had any success?

Many thanks,

The common way is to encapsulate the part with a variable number of fields e.g. in a CustomField so that it is a single component from the point of view of the Form. Usually this also matches the underlying item structure - the variable number of fields maps to a single collection in the underlying POJO.

If this is not ok for you, you can either bypass the use of Form and handle the logic yourself (using FormLayout or any other layout) or call Form.addItemProperty()/Form.addField() explicitly if the underlying item has the property - it can be added to the item if using e.g. PropertySetItem. There might be some complications with the latter approach, though, so make sure you test your code if using this approach.