Best practice to render dynamic flask WTForm FieldList (with nesting)

Viewed 266

This has been asked a lot but no clear answer so far.

Is there an "official" way or an extension to render WTForm FieldList with the ability to add/remove fields with buttons?

I find it weird that no one has normalized that so far. All the answers I found (here, here or here) are hand-written JavaScript that would break if:

  1. There is 0 field (a "template" is needed to clone the items)
  2. FieldList/FormField get nested

I did write a solution as well but as soon as I tried to make it generic (to handle nesting and applicable to multiple forms), it becomes almost impossible to handle. I'm trying to find a generic solution that would work on all my forms to avoid having to maintain them all "field by field", any ideas?

Example of form:

- field1 = StringField
- field2 = FieldList
    - field2_1 = StringField
- field3 = FieldList
    - field3_1 = FormField
        - field3_1_1 = StringField
        - field3_1_2 = FieldList
            - field3_1_2_1 = StringField
    - field2_2 = StringField
- field2_2 = StringField

Bonus question: A solution that is Boostrap4 friendly?

0 Answers
Related