Cyrlop
Cyrlop

Reputation: 1984

Load data in nested FieldList/FormField in flask forms (WTForm)

I have a problem initiating/populating a Form with nested data.

The form is as follow:

field_1 --> FieldList(FormField)
    field_1_1 --> StringField
    field_1_2 --> StringField
    field_1_3 --> FieldList(StringField)

I want to instanciate the form using my_form = my_form_class(**data).

Here is what data looks like:

data = {
    "field_1 ": [
        "field_1_1": "abc",
        "field_1_2": "abc",
        "field_1_3": ["abc", "abc", "abc"]
    ]
}

But after after instantiation, my_form.data is empty and my_form.validate() doesn't pass.

I have other similar forms that are just one level less nested (no FieldList inside a FormField) and it works fine, could this be the problem?

This person is having the same problem (no solution). From searching around, I see people talking about multiple CSRF tokens? How does this work?

Any ideas?

Upvotes: 0

Views: 347

Answers (1)

Cyrlop
Cyrlop

Reputation: 1984

The solution was to use:

my_form = my_form_class()
my_form.process(data=form_data_json)

instead of:

my_form = my_form_class(**data)

Check this part of the documentation about the .process() method:

Since BaseForm does not take its data at instantiation, you must call this to provide form data to the enclosed fields.

Upvotes: 0

Related Questions