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.
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__
):You might need to set
a.data
to a certain value. If the field is aSelectField
, you might even need to add a_value
callable to the field.You can do something like
Although it can be done in views too like you did, but it is better to keep form logic in your Form class.