Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms FieldList with optional fields

I have the following form,

class AddForm(wtf.Form):
    tags = TagListField("Tags (comma separated)", validators=[wtf.Required()])
    question = wtf.TextField("Question", validators=[wtf.Required()])
    answers = wtf.FieldList(wtf.TextField("Answer", validators=[wtf.Required()]), min_entries=2, max_entries=5)

And I have a form setup to display this form along with a button that adds more "answers" inputs dynamically (by the user clicking a button). However, when the form gets submitted, any fields that are added but not filled in are considered errors.

Specifically, if I have 3 inputs for "Answer", but I only fill in the first two, then the third one comes up as an error, even though I have specified that the minimum number of entries is 2. It seems like it should ignore this data.

Since I am using this with Flask I am going to just modify the request.form data to ignore blank fields. Is there something I'm missing?

like image 913
milkypostman Avatar asked Oct 24 '25 04:10

milkypostman


1 Answers

May be your the

 validators=[wtf.Required()])

is an issue ? Have you tried changing it to

validators=[wtf.Optional()])
like image 146
codegeek Avatar answered Oct 26 '25 18:10

codegeek