Flask-Security自定义电子邮件验证器

时间:2020-06-30 13:56:35

标签: flask flask-wtforms wtforms flask-security

我已经设置了Flask-Security,并且试图通过电子邮件对数据库中的列表进行验证。 我想保留当前的电子邮件验证程序并添加一个新的验证程序。

下面是我添加的用于扩展注册表格的代码。但是我根本无法使它正常工作,只是忽略了验证器。 我尝试过

  • 更改所需数据以输入所需内容,
  • 改变自我的形式。
  • 在验证器中添加打印语句,但是它似乎根本没有运行。只是被忽略了
  • 将StringField更改为EmailField
class ExtendedRegisterForm(RegisterForm):
    email = StringField('Email', validators=[DataRequired()])

    def validate_email(form, field):
        domain = field.data.split('@')[1]
        regdomains = db.session.query(Company.id, Company.domain).filter(Company.domain != None).all()
        for row in regdomains:
            valid = (row.domain).find(domain)
            if valid != -1:
                validated = True
            else:
                continue
        if validated is not True:
            raise ValidationError('Email address must be from an authorised domain')
security = Security(app, user_datastore, register_form=ExtendedRegisterForm)

3 个答案:

答案 0 :(得分:0)

我假设您已经安装了React.cloneElement(child, { key: index, className: `${child.props.className} ${ window.location.pathname.includes(child.props.href) ? "active" : "" }` }); email_validator上的link

我认为您需要像这样更改代码

pypi

from flask_wtf import FlaskForm from flask_security.forms import RegisterForm [..] from wtforms.fields.html5 import EmailField from wtforms.validators import DataRequired, Email, .. # @see https://pythonhosted.org/Flask-Security/customizing.html#forms class ExtendedRegisterForm(RegisterForm): # email = StringField('Email', validators=[DataRequired()]) email = EmailField('Email', validators=[DataRequired(), Email()]) # def validate_email(form, field): def validate_email(self, field): # extract the domain from email domain = field.data.split('@')[1] # regdomains = db.session.query(Company.id, Company.domain).filter(Company.domain != None).all() # for row in regdomains: # valid = (row.domain).find(domain) # if valid != -1: # validated = True # else: # continue # if validated is not True: # raise ValidationError('Email address must be from an authorized domain') # check against database if any "company" is registered with the given "domain" else raise an Exception registred_company = Company.query.filter_by(domain=domain).first() if registred_company is None: # the "company" is not registered thus not authorized # then the given "email" is not validated raise ValidationError('Email address must be from an authorised domain') 具有flask-security(取决于flask-wtf)作为依存关系,因此它通过关于{{1}的继承机制依赖于wtforms }和wtforms函数来验证表单字段。

请参阅:https://github.com/mattupstate/flask-security/blob/develop/flask_security/forms.py

答案 1 :(得分:0)

覆盖RegisterForm validate(self)方法。您可以在LoginForm的{​​{3}}中看到此技术的示例。

示例:

class ExtendedRegisterForm(RegisterForm):

    def validate(self):

        # Perform the default validation
        if not super(ExtendedRegisterForm, self).validate():
            return False

        # check against database if any "company" is registered with the given "domain" else raise an Exception

        # extract the domain from email
        domain = self.email.data.split('@')[1]

        registered_company = Company.query.filter_by(domain=domain).first()
        if registered_company is None:
            # the "company" is not registered thus not authorized 
            # then the given "email" is not validated
            # append error message to the email field
            self.email.errors.append("Company is not registered")
            return False

        #  all good
        return True

答案 2 :(得分:0)

您是否有可能启用了SECURITY_CONFIRMABLE?如果是这样,则注册端点将使用ConfirmRegisterForm。