如何在wtform validate_中获取另一个字段的数据?

how to get data of another field in wtform validate_?

我正在尝试像这样在 wtform 上进行密码验证:

email = StringField('Email', validators=[DataRequired(), Email()])
def validate_password(self, password):
        print(self)
        user = dbSession.execute(
            "SELECT * FROM users WHERE email = :email",
            {"email": self.email.data}
        ).fetchone()
        print(user)
        if not bcrypt.check_password_hash(user.password, password.data):
            raise ValidationError('Incorrect Password.')

我想从其他领域获取电子邮件,但我想它不起作用,我试过 email.data 但它没有定义。另外,它没有登录控制台。在 js 中,你记录一个这样的对象。我想查看自己的属性和用户,我想在 python.

中这样登录
console.log('user =', user);

帮助?

这个答案重新表述 this little snippet

Sometimes you are in the situation where you need to validate a form with custom logic that can not necessarily be reduced to a validator on a single field. A good example are login forms where you have to make sure a user exists in the database and has a specific password.

class LoginForm(Form):
    email = TextField('Email', [validators.InputRequired(), Email()])
    password = PasswordField('Password', [validators.InputRequired()])

    def __init__(self, *args, **kwargs):
        Form.__init__(self, *args, **kwargs)
        self.user = None

    def validate(self):
        rv = Form.validate(self)
        if not rv:
            return False

        user = User.query.filter_by(
            email=self.email.data).first()
        if user is None:
            self.email.errors.append('Unknown email')
            return False

        if not bcrypt.check_password_hash(user.password, self.password.data):
            self.password.errors.append('Invalid Password')               
            return False

        self.user = user
        return True