WTForm自定义内联验证器

时间:2019-12-01 19:58:16

标签: python flask flask-wtforms

我想为重复的用户名和电子邮件提供一个内联验证器。但是,它仅在我首先将is定义为一个函数并将其放入验证器列表时才起作用。 以下是我的代码

这是烧瓶

from project import app, db
from project.forms import Reg_form
from project.models import User
from flask import render_template, flash, session, redirect, url_for
from flask_login import login_required

@app.route('/register', methods=['GET','POST'])
def register():
    form = Reg_form()
    if form.validate_on_submit():
            new_user = User(email = form.email.data,
                            username = form.username.data,
                            password = form.password.data)
            db.session.add(new_user)
            db.session.commit()
            flash('Thanks for registering, you may login now.')
            return redirect(url_for('home'))
    return render_template('register.html', form = form)

if __name__ == '__main__':
    app.run(debug=True)

这是表格

from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField, PasswordField
from wtforms.validators import data_required, email, equal_to, length, ValidationError
from project.models import User
def check_email(self, email):
        if User.query.filter_by(email=email.data).first():
            raise ValidationError('Email has been registered')

def check_username(self, username):
        if User.query.filter_by(username=username.data).first():
            raise ValidationError('Username has been registered')

class Reg_form(FlaskForm):
    email = StringField('Email', validators=[data_required(), email(), check_email], render_kw={"placeholder": "your@email.com"})
    username = StringField('Username', validators=[data_required(), check_username], render_kw={"placeholder": "Your username"})
    password = PasswordField('Password', validators=[data_required(), equal_to('confirm_pw', message='Password must match with the confirmed password.')], render_kw={"placeholder": "Your password"})
    confirm_pw = PasswordField('Confirm password', validators=[data_required()], render_kw={"placeholder": "Confirm your password"})
    submit = SubmitField('Register.')

这是我想要的但不起作用

from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, SubmitField, PasswordField
from wtforms.validators import data_required, email, equal_to, length, ValidationError
from project.models import User

class Reg_form(FlaskForm):
    email = StringField('Email', validators=[data_required(), email()], render_kw={"placeholder": "your@email.com"})
    username = StringField('Username', validators=[data_required()], render_kw={"placeholder": "Your username"})
    password = PasswordField('Password', validators=[data_required(), equal_to('confirm_pw', message='Password must match with the confirmed password.')], render_kw={"placeholder": "Your password"})
    confirm_pw = PasswordField('Confirm password', validators=[data_required()], render_kw={"placeholder": "Confirm your password"})
    submit = SubmitField('Register.')
    def check_email(self, email):
            if User.query.filter_by(email=email.data).first():
                raise ValidationError('Email has been registered')

    def check_username(self, username):
            if User.query.filter_by(username=username.data).first():
                raise ValidationError('Username has been registered')

谢谢。

1 个答案:

答案 0 :(得分:0)

这是因为自定义验证器必须以validate_开头。实际上,我发现很难在文档中找到准确的位置(例如,在我认为值得提及的地方here中没有明确提及),但是您可以在the implementation中看到它:

def validate(self):
    """
    Validates the form by calling `validate` on each field, passing any
    extra `Form.validate_<fieldname>` validators to the field validator.
    """
    extra = {}
    for name in self._fields:
        inline = getattr(self.__class__, "validate_%s" % name, None)
        if inline is not None:
            extra[name] = [inline]

    return super(Form, self).validate(extra)

因此,您需要将check_email更改为validate_email等。