flask-wtf validate_on_submit() returns 始终为真
flask-wtf validate_on_submit() returns always true
我是 flask-wtf 的新手。我根据文档做了,但是 validate_on_submit() returns 总是正确的,没有按照规则进行验证。我是否遗漏了什么或做错了什么?
表格:
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, BooleanField
from wtforms.validators import DataRequired, Length, Optional
class TemplateCreateForm(FlaskForm):
title = StringField([Length(min=4, max=45), DataRequired()])
view_path = StringField([Length(min=1, max=45), DataRequired()])
caption = TextAreaField([Length(min=5, max=250), Optional()])
is_active = BooleanField([DataRequired()])
controller.py
@submodule.route('/create', methods=['GET', 'POST'])
@logged_user_only
def create():
form = TemplateCreateForm(request.form)
if form.validate_on_submit():
mongo.db.template.insert(
{
'title': str(form.title.data),
'is_active': bool(form.is_active.data),
'caption': str(form.caption.data),
'view_path': str(form.view_path.data),
'user_id': str(session['user.user_id']),
'insert_timestamp': datetime.now()
}
)
print form.errors
return render_template('create.html', form=form)
view.html
<form action="{{%20url_for('template.create')%20}}" method="post">
<h5 class="form-header">Template creation</h5>{{ form.hidden_tag() }} {{ form.csrf_token }}
<div class="form-group">
<label for="">Title *</label> {{ form.title(class_='form-control') }}
</div>
<div class="form-group">
<label for="">Caption </label> {{ form.caption(class_='form-control') }}
</div>
<div class="form-group">
<label for="">View *</label> {{ form.view_path(class_='form-control') }}
</div>
<div class="form-check">
<label class="form-check-label" for="">{{ form.is_active(class_='form-check-input') }} Active</label>
</div>{% for message in form.errors %} {{ message }} {% endfor %}
<div class="form-buttons-w">
<button class="btn btn-primary">Submit</button>
</div>
</form>
您在 class TemplateCreateForm
中有错误。字段应按以下格式描述:
field = StringField('label_here', list_of_validators_here)
但在您的情况下 list_of_validators
相当于 label
。所以,只需将标签添加到表单的所有字段即可。
title = StringField('title', [Length(min=4, max=45), DataRequired()])
view_path = StringField('view_path', [Length(min=1, max=45), DataRequired()])
...
我是 flask-wtf 的新手。我根据文档做了,但是 validate_on_submit() returns 总是正确的,没有按照规则进行验证。我是否遗漏了什么或做错了什么?
表格:
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, BooleanField
from wtforms.validators import DataRequired, Length, Optional
class TemplateCreateForm(FlaskForm):
title = StringField([Length(min=4, max=45), DataRequired()])
view_path = StringField([Length(min=1, max=45), DataRequired()])
caption = TextAreaField([Length(min=5, max=250), Optional()])
is_active = BooleanField([DataRequired()])
controller.py
@submodule.route('/create', methods=['GET', 'POST'])
@logged_user_only
def create():
form = TemplateCreateForm(request.form)
if form.validate_on_submit():
mongo.db.template.insert(
{
'title': str(form.title.data),
'is_active': bool(form.is_active.data),
'caption': str(form.caption.data),
'view_path': str(form.view_path.data),
'user_id': str(session['user.user_id']),
'insert_timestamp': datetime.now()
}
)
print form.errors
return render_template('create.html', form=form)
view.html
<form action="{{%20url_for('template.create')%20}}" method="post">
<h5 class="form-header">Template creation</h5>{{ form.hidden_tag() }} {{ form.csrf_token }}
<div class="form-group">
<label for="">Title *</label> {{ form.title(class_='form-control') }}
</div>
<div class="form-group">
<label for="">Caption </label> {{ form.caption(class_='form-control') }}
</div>
<div class="form-group">
<label for="">View *</label> {{ form.view_path(class_='form-control') }}
</div>
<div class="form-check">
<label class="form-check-label" for="">{{ form.is_active(class_='form-check-input') }} Active</label>
</div>{% for message in form.errors %} {{ message }} {% endfor %}
<div class="form-buttons-w">
<button class="btn btn-primary">Submit</button>
</div>
</form>
您在 class TemplateCreateForm
中有错误。字段应按以下格式描述:
field = StringField('label_here', list_of_validators_here)
但在您的情况下 list_of_validators
相当于 label
。所以,只需将标签添加到表单的所有字段即可。
title = StringField('title', [Length(min=4, max=45), DataRequired()])
view_path = StringField('view_path', [Length(min=1, max=45), DataRequired()])
...