Flask IntegerField 小部件验证器不工作
Flask IntegerField widget validator not working
我可能遗漏了一些明显的东西,但我无法让它正常工作。我想要的是 "spinner" 小部件停在最大(最小)值处并且不允许用户选择更高(更低)的值。我做错了什么?
观点:
<html>
<head>
<title>Widget Question</title>
</head>
<body>
<form method=post action=''>
{{ template_form.my_intfield.label }} {{ template_form.my_intfield }}
</form>
</body>
</html>
控制器:
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)
我没有收到错误,只是没有正常工作;即,微调器不会停在 100,它会继续运行。我希望它在顶端停在 100 并且在底端不允许负数。
WTForms 验证在服务器端完成。您还需要添加 JavaScript 验证 - 例如 Parsley JS.
我可能遗漏了一些明显的东西,但我无法让它正常工作。我想要的是 "spinner" 小部件停在最大(最小)值处并且不允许用户选择更高(更低)的值。我做错了什么?
观点:
<html>
<head>
<title>Widget Question</title>
</head>
<body>
<form method=post action=''>
{{ template_form.my_intfield.label }} {{ template_form.my_intfield }}
</form>
</body>
</html>
控制器:
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)
我没有收到错误,只是没有正常工作;即,微调器不会停在 100,它会继续运行。我希望它在顶端停在 100 并且在底端不允许负数。
WTForms 验证在服务器端完成。您还需要添加 JavaScript 验证 - 例如 Parsley JS.