调用FieldList和FormField validate()的Flask Wtform会导致错误

时间:2016-02-12 10:46:08

标签: python validation flask wtforms flask-wtforms

所以我有这段代码

class ItemPurchaseForm(wtforms.Form):
    purchase_price = IntegerField(label='Purchase Price: ',
                                  validators=[InputRequired()])

    def validate(self, *args, **kwargs):
        if not super().validate():
            self.purchase_price.errors += (super().errors,)
            return False
        #.... do other validations....

class PurchaseTransactionForm(Form):
    yyyy = IntegerField(label='Transaction Year',
                        validators=[InputRequired()])

    transaction_items = FieldList(FormField(ItemPurchaseForm),
                                  label='Purchased items',
                                  min_entries=1)
    submit_button = SubmitField(label='Add new purchase transaction')

    def validate(self, **kwargs):

        if not super().validate():
            self.yyyy.errors += (super().errors, 'super not validated')
            return False

         #.... do some other validation

正如您所看到的那样FieldList(FormField(...)),所以我按照answer来修复CRSF字段导致的上一个错误。现在,如果我def validate()中有ItemPurchaseForm,它会给我错误,错误就是这样:{'transaction_items': [{}]}

如果我从def validate()移除ItemPurhcaseForm,一切正常。有什么理由导致错误吗?我确实读过这个http://wtforms.readthedocs.org/en/latest/fields.html#wtforms.fields.Field.validate,这是否意味着ItemPurchaseForm是一个子域?

1 个答案:

答案 0 :(得分:2)

我知道问题并解决了问题。我忘了将return True放在ItemPurchaseForm validate()