DataRequired()验证不适用于烧瓶

时间:2019-06-06 17:42:41

标签: python flask flask-wtforms

我使用wtforms.validators来验证表单字段。我已经在字段上进行了DataRequired验证检查,但是当我直接提交表单而未在表单字段中输入任何值时,即时通讯没有收到wtforms验证错误,而是出现了HTML错误,并显示错误消息“请填写此字段”

forms.py

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo

class RegistrationForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=2, max=15)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password',validators=[DataRequired(), Length(min=8)])
    confirm_password = PasswordField('Confirm Password',validators=[DataRequired(), EqualTo('password')])
    submit = SubmitField('Sign Up')

flaskblog.py

def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('home'))
    return render_template('register.html', title='Register', form=form)

register.html

<div class="form-group">
                    {{ form.username.label(class='form-control-label')}}
                    {% if form.username.errors %}
                        {{ form.username(class='form-control form-control-lg is-invalid')}}
                        <div class="invalid-feedback">
                            {% for error in form.username.errors %}
                                <span>{{error}}</span>
                            {% endfor %}
                        </div>
                    {% else %}
                        {{ form.username(class='form-control form-control-lg')}}
                    {% endif %}

在register.html中,其他字段也是如此

0 个答案:

没有答案