WTF表格(Flask) - FormField - FieldList,验证不起作用

时间:2016-03-04 08:00:30

标签: flask flask-wtforms

尝试使用WTF字段,FormField,FieldList:

创建ListItem表单

forms.py

class WarehouseTicketItemForm(Form):
      description = StringField(u'Description', validators=[Required()])
      quantity = IntegerField(u'Quantity', validators=[Required()])

class WarehouseTicketForm(Form):
      spot_id = SelectField(u'Spot', coerce=int)
      items = FieldList(FormField(WarehouseTicketItemForm), min_entries = 3)

warehouse_ticket_form.html

<form action="" method='POST'>
  {{ form.hidden_tag() }}
  {{ form.spot_id }} <br>
  {% for item in form.items %}
    {{ item }}
  {% endfor %}
 <button type="submit" name="button">Save it</button>

warehouse_tickets.py

@coordinator.route('/warehouses/tickets/update', methods=['GET', 'POST'])
def update_warehouses_tickets():
   page = dict(title="Warehouse Ticket")
   form = WarehouseTicketForm()
   form.spot_id.choices = [(s.id, s.name)
                           for s in Spot.query.filter_by(status=True)]
   if form.validate_on_submit():
       print form.data
   return render_template('coordinators/warehouses/warehouse_ticket_form.html',
                        page=page, form=form)

提交表格&#34; print form.data&#34;没有任何回报......

我无法弄清楚我错过了

1 个答案:

答案 0 :(得分:1)

我将验证作为项目字段的参数。

class WarehouseTicketForm(Form):
    spot_id = SelectField(u'Spot', coerce=int)
    items = FieldList(FormField(WarehouseTicketItemForm), min_entries = 3, validators=[Required()])