简体   繁体   English

带有可选字段的WTForms FieldList

[英]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. 具体来说,如果我有3个输入为“答案”,但我只填写前两个,那么第三个出现为错误,即使我已指定最小条目数为2.它似乎应该忽略这些数据。

Since I am using this with Flask I am going to just modify the request.form data to ignore blank fields. 由于我在Flask中使用它,我将修改request.form数据以忽略空白字段。 Is there something I'm missing? 有什么我想念的吗?

May be your the 可能是你的

 validators=[wtf.Required()])

is an issue ? 是一个问题? Have you tried changing it to 您是否尝试过将其更改为

validators=[wtf.Optional()])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM