Python Flask wt forms ventilator check if password is matching database
<p>I am trying to create a from where before updating records the user must type in their password for validation.</p>

<p>I was thinking of useing something like this.</p>

<pre><code>class AccountSettingsForm(Form):
        password_proof= TextField("Password:",[validators.EqualTo(current_user.password, message='Passwords Invlaid')])
</code></pre>

<p>But i get this error<br>
AttributeError: 'NoneType' object has no attribute 'password'</p>

<p>You need to create a validator method with the syntax <code>validate_{field_name}</code>. Also, as you are using other data (the user instance, which contains their password), you need to initialize the form with that user instance.</p>

<p>Something like this should work for your example:</p>

<pre><code>from wtforms import ValidationError

class AccountSettingsForm(Form):
    password_proof= TextField("Password:")

    def __init__(self, user, *args, **kwargs):
        super(AccountSettingsForm, self).__init__(*args, **kwargs)
        self.user = user

    def validate_password_proof(self, field):
        if field.data != self.user.password:
            raise ValidationError('Wrong password.')
</code></pre>

<p>Then, when initializing the form, you need to do it like this:</p>

<pre><code>form = AccountSettingsForm(current_user)
</code></pre>

<p>On an unrelated side note, you should encrypt your users' passwords if you are not doing so.</p>

python flask flask-sqlalchemy flask-wtforms flask-security