title: Password Validation Not Working For Flask Form
body: <p>I'm trying to implement a form where I want a message to be shown in advance if passwords don't match. I have added a data required validator but password matching validator isn't working.</p>
  <p>Here's how a data required validator looks like, I want a similar password matching validator.</p>
  <p><a href="https://i.stack.imgur.com/ALqdx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ALqdx.png" alt="enter image description here" /></a></p>
  <p>My <strong>forms.py</strong> file:</p>
  <pre><code>from flask_wtf import FlaskForm
  from wtforms import StringField, PasswordField, TextField, validators
  from wtforms.validators import DataRequired, Email, EqualTo
  # import validators
  
  class PasswordForm(FlaskForm):
      password = PasswordField('Password', validators=[validators.DataRequired(), validators.EqualTo('confirm', message='Passwords must match')])
      confirm = PasswordField('Confirm Password', validators=[validators.DataRequired()])
  </code></pre>
  <p>My <strong>reset_with_token.html</strong>:</p>
  <pre><code>{% extends &quot;base.html&quot; %}
  {% block content %}
  &lt;div id=&quot;wrapper&quot;&gt;
      &lt;div style=&quot;margin: 0;
      padding-left: 20px;
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    font-size: x-large;
    /* height: 100%; display: flex; flex-direction: column;
              align-items: center */
    &quot;&gt;
          &lt;form action=&quot; {{ url_for('reset_with_token', token=token) }}&quot; method=&quot;POST&quot;&gt;
              &lt;div class=&quot;form-group&quot;&gt;
                  {{ form.password.label }}:
                  &lt;br&gt;
                  {{ form.password}}
                  &lt;br&gt;
                  {{ form.confirm.label }}:
                  &lt;br&gt;
                  {{ form.confirm}}
                  {{ form.csrf_token }}
              &lt;/div&gt;
              &lt;input type=&quot;submit&quot; class=&quot;btn btn-primary block full-width m-b&quot; value=&quot;Change my password&quot; /&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  
  
  {% endblock %}
  </code></pre>
  <p>The <strong>reset_with_token</strong> route:</p>
  <pre><code>@app.route('/api/reset/&lt;token&gt;', methods=[&quot;GET&quot;, &quot;POST&quot;])
  def reset_with_token(token):
      ts = URLSafeTimedSerializer(app.config[&quot;SECRET_KEY&quot;])
      try:
          email = ts.loads(token, salt=&quot;recover-key&quot;, max_age=86400)
          print('CORRECT REACHED')
      except:
          print('ERROR')
          abort(404)
  
      form = PasswordForm()
  
      # if db.session.query(user_table).filter_by(email=email).count() &gt; 0:
      if form.validate_on_submit():
          # user = User.query.filter_by(email=email).first_or_404()
          user = db.session.query(user_table).filter_by(email=email).first_or_404()
  
          # user.password = form.password.data
          user.password = guard.hash_password(form.password.data)
          # guard.hash_password('password')
  
          db.session.add(user)
          db.session.commit()
  
          # return redirect(url_for('signin'))
          return 'YES'
      else:
          flash('Passwords should match')
  
      return render_template('reset_with_token.html', form=form, token=token)
  </code></pre>
  <p>Am I missing something here? I'm very new to Flask.</p>