Kot Vladislav
Kot Vladislav

Reputation: 1

Active button in WTForms Flask SelectField with dynamic choices

Im trying to add into the dynamic choices of WTForms Flask SelectField 1 static option. At this moment I have a category_list, which one works by query in to the db, and include all avaliable choices for each user. I want to add in this selectfield 1 more active option with some action, like a button in bootstrap dropdown with 'href'. Here is SelectField code in form:

class TransactionForm(FlaskForm):
    category = SelectField(coerce=int,
                           validators=[InputRequired()],
                           render_kw={"class": "btn btn-primary dropdown-toggle",
                                      "data-toggle": "dropdown",
                                      "aria-haspopup": "true",
                                      "aria-expanded": "false"
                                      }
                           )

Would you be so kind to help me find a way to do this, or explain me, why it is impossible?

Upvotes: 0

Views: 273

Answers (1)

Edoardo Vignati
Edoardo Vignati

Reputation: 553

Remember to put also the available choices when setting up the Form class.

class MyForm(FlaskForm):
    sel = SelectField(coerce=int, label="MyLabel", choices=my_choices)
    ...

where choices can be something like this:

my_choices = [x.id for x in get_products()]

An exception is arised in function pre_validate in script /wtforms/fields/core.py when the validate_on_submit() functions is called

Upvotes: 0

Related Questions