Nested objects using WTForms (or FormEncode)?

355 Views Asked by At

What's the best practice for creating a form with nested objects/fields?

I have a Team object, with a field "name", and a team can consist of 1 to many (lets say 12) members, each of whom has a name, an e-mail, age, t-shirt size etc. I'd like to validate all of them in one go. So that I would post all the team members as an array.. the field names would be something like members[0].name members[1].name or whatever the form validator would be able to parse.

1

There are 1 best solutions below

0
On BEST ANSWER

Create one Form for the Team and one Form for a Member. In the Team-form, create a FieldList of FormFields:

class Member(Form):
    name = StringField("Name")
    ...

class Team(Form):
    ...
    members = FieldList(FormField(Member))