2

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?

Cyrlop
  • 1,894
  • 1
  • 17
  • 31
  • 1
    It's not really a very satisfying answer, but as someone with a fair amount of flask-wtf experience, I would say there isn't a canonical or even very good way of doing this kind of dynamic stuff in WTForms. – Ken Kinder Jun 22 '20 at 17:42
  • Thanks, I feel the reason for that is that if you need dynamic nested lists of variable length that means the design is bad in the first place. That still seems a kind of "basic" need. – Cyrlop Jun 26 '20 at 09:41

0 Answers0