Flask, WTForms: Is there a way to make a StringFie

2019-07-12 20:05发布

问题:

This is my model:

class F(Form):
    a = StringField('a', validators = [validators.DataRequired()])

Is there a way to make a StringField in the form temporarily hidden? Something like:

@app.route('/f', methods = ['GET', 'POST'])
def f():
    form = F(request.form)
    if foo(form):
        form.a.__MakeTemporarilyHidden__()
    else:
        form.a.__MakeItVisibleAgain__()
    if request.method == 'GET':
        return render_template('f.html', form = form)

I am aware of wtforms.fields.HiddenField but I want to dynamically switch between StringField and HiddenField and back.

回答1:

You can do something like

class F(Form):
    a = StringField('a', validators = [validators.DataRequired()])

    def __init__(self, *args, **kwargs):
        hide_a = kwargs.pop('hide_a')
        super(F, self).__init__(*args, **kwargs)
        if hide_a:
            self.a.widget = widgets.HiddenInput()

@app.route('/f', methods = ['GET', 'POST'])
def f():
    if foo():
        form = F(request.form, hide_a=True)
    else:
        form = F(request.form)
    if request.method == 'GET':
        return render_template('f.html', form = form)

Although it can be done in views too like you did, but it is better to keep form logic in your Form class.



回答2:

Af far as I've found, it's not a good idea to change the field to a HiddenField.

What you can do however is to leave the field as it is, but replace its widget so that it's rendered as hidden. The relevant widget is wtforms.widgets.HiddenInput.

Note: this was done on Python3.

Extend your form class with a hide_a() method (what you were calling __MakeTemporarilyHidden__):

from wtforms.widgets import HiddenInput

class F(Form):
    a = StringField('a', validators = [validators.DataRequired()])

def hide_a(self):
    a.widget = HiddenInput()

You might need to set a.data to a certain value. If the field is a SelectField, you might even need to add a _value callable to the field.