SubmitField需要验证器吗?

时间:2012-10-16 04:55:06

标签: python wtforms

我是WTForms的新手。我有一个带有提交按钮的用户注册表单:

submit = SubmitField('Submit')

我想确保通过按提交按钮而不是其他方式提交表单。在docs中说明了

class wtforms.fields.SubmitField(default field arguments)
Represents an <input type="submit">. This allows checking if a given submit button has been pressed.

提出了一个问题:对于我的情况,我是否需要添加“必需”验证器?

例如,是否需要:

submit = SubmitField('Submit', [validators.Required()])

1 个答案:

答案 0 :(得分:4)

我想你做到了。查看源代码,不需要SubmitField

SubmitField实际上只是BooleanField

class SubmitField(BooleanField):
    """
    Represents an ``<input type="submit">``.  This allows checking if a given
    submit button has been pressed.
    """
    widget = widgets.SubmitInput()

关联的窗口小部件只是一个输入窗口小部件,其type =“submit”。它似乎也使用SubmitField的标签作为HTML中的文本。

class SubmitInput(Input):
    """
    Renders a submit button.

    The field's label is used as the text of the submit button instead of the
    data on the field.
    """
    input_type = 'submit'

    def __call__(self, field, **kwargs): 
        kwargs.setdefault('value', field.label.text)
        return super(SubmitInput, self).__call__(field, **kwargs)

它似乎没有比这更多。