I have a WTForm
class like so:
class MyForm(Form):
field1 = HiddenField(default=0, validators=NumberRange(min=0, max=20)])
consider this markup as rendered by WTForms
<input type='hidden' name='field1' value='5'></input>
This does not pass the NumberRange
validation. This is because HiddenField
s widget class coerces the value
attribute into a string. How can I get WTForms
to produce this markup such that I can perform numeric validation on the subsequent POST
?
The recommended trick is to use an IntegerField
and change the widget to a HiddenInput
class MyForm(Form):
field1 = IntegerField(widget=HiddenInput())
you can also subclass
class HiddenInteger(IntegerField):
widget = HiddenInput()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With