Reputation: 87
I'm likely missing something obvious, but I can't get this to work properly. What I want is for the "spinner" widget to stop at the max (min) value and not allow the user to pick a higher (lower) value. What am I doing wrong?
The view:
<html>
<head>
<title>Widget Question</title>
</head>
<body>
<form method=post action=''>
{{ template_form.my_intfield.label }} {{ template_form.my_intfield }}
</form>
</body>
</html>
The controller:
from flask import Flask, render_template, request
from wtforms import Form, IntegerField, widgets, validators
app = Flask(__name__)
app.secret_key='some secret'
class InputForm(Form):
# This is the problem line - I can spin past max or min.
my_intfield = IntegerField(label='My integer field', widget=widgets.Input(input_type='number'), validators=[validators.InputRequired(), validators.NumberRange(min=0, max=100)], default=100)
@app.route('/', methods=['GET', 'POST'])
def index():
form=InputForm(request.form)
return render_template('view.html', template_form=form)
if __name__ == '__main__':
app.run(debug=True)
I don't get an error, it just doesn't work properly; i.e., the spinner doesn't stop at 100, it keeps on going. I'd like it to stop at 100 on the top end and not allow negative numbers on the bottom end.
Upvotes: 0
Views: 650
Reputation: 8046
WTForms validation is done on the server side. You'll need to add JavaScript validation too - such as Parsley JS.
Upvotes: 1