title: Flask WTForms custom validators not working
body: <p>So I'm trying to implement custom validators in my Flask App, and I'm running into a problem. The custom validator I wrote for RegisterForm, UniqueRequired() seems to be working, but my validators for LoginForm don't even seem to be being called at all, with the print statement in ValidUsername() not printing. I've already tried putting the validators in the same file, importing them in the different statements, and changing the order of the validators, but nothing works.</p>
  <p>Here is my forms.py file:</p>
  <pre><code>from flask_wtf import FlaskForm
  from wtforms import StringField, PasswordField
  from wtforms.validators import DataRequired, Length, EqualTo
  from validators import UniqueRequired, VaildUsername, ValidPassword
  
  class LoginForm(FlaskForm):
      username = StringField(&quot;Username:&quot;, validators=[DataRequired(), VaildUsername], id=&quot;username&quot;)
      password = PasswordField(&quot;Password:&quot;, validators=[DataRequired(), ValidPassword], id=&quot;password&quot;)
  
  class RegisterForm(FlaskForm):
      username = StringField(&quot;Username:&quot;, validators=[DataRequired(), UniqueRequired, Length(min=5, max=25)], id=&quot;username&quot;)
      password = PasswordField(&quot;Password:&quot;, validators=[DataRequired(), Length(min=8)], id=&quot;password&quot;)
      confirmpassword = PasswordField(&quot;Confirm Password:&quot;, validators=[DataRequired(), EqualTo('password', message=&quot;Passwords don't match&quot;)], id=&quot;conpassword&quot;)
  </code></pre>
  <p>My validators.py file:</p>
  <pre><code>def UniqueRequired(form, field):
      otheruser = User.query.filter_by(username=field.data).first()
      if otheruser:
          raise ValidationError(&quot;Username Taken&quot;)
  
  def VaildUsername(form, field):
      user = User.query.filter_by(username=field.data).first()
      print(user)
      if not user:
          raise ValidationError(&quot;Invalid Username&quot;)
  
  def ValidPassword(form, field):
      user = User.query.filter_by(username=form.username.data).first()
      if not check_password_hash(user.password, field.data):
          raise ValidationError(&quot;Incorrect Password&quot;)
  
  </code></pre>
  <p>and my /login and /register routes:</p>
  <pre><code>@app.route(&quot;/login&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
  def login():
      form = LoginForm()
      if form.validate_on_submit:
          print(&quot;Form Valid&quot;)
      return render_template(&quot;login.html&quot;, form=form)
  
  @app.route(&quot;/register&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
  def register():
      form = RegisterForm()
      if form.validate_on_submit():
          print(&quot;Form Valid&quot;)
      return render_template(&quot;register.html&quot;, form=form)
  </code></pre>