Forums

Flask wtforms custom validators not working on 2 forms on a single page

So I have a login form and a signup form on home.html. I have custom validators set up on my signup form that when a user tries to sign up with a username or email that already exists, it will show error on the form. But when I do this, it doesn't show me any error. It works on a different route where I have only one form.

Below is the forms.py file:

class RegistrationForm(FlaskForm):
    username_regis = StringField('Username',
                       validators=[DataRequired(), Length(min=5, max=20)])
    email_regis = EmailField('Email',
                    validators=[DataRequired(), Email()])
    password_regis = PasswordField('Password', validators=[DataRequired()])
    submit_regis = SubmitField('Sign Up')

def validate_username_regis(self, username_regis):
    user = User.query.filter_by(username=username_regis.data).first()
    if user:
        raise ValidationError('That username is taken. Please choose a different one.')

def validate_email_regis(self, email_regis):
    user = User.query.filter_by(email=email_regis.data).first()
    if user:
        raise ValidationError('That email is taken. Please choose a different one.')

Below are the routes.py functions for main and signup:

@app.route("/", methods=['GET', 'POST'])
def home():
    login_form = LoginForm(request.form, prefix="login-form")
    regis_form = RegistrationForm(request.form, prefix="register-form")
    events_main = Event.query.order_by(Event.date_posted.desc())
return render_template('home.html', login_form=login_form, regis_form=regis_form)

@app.route("/register", methods=['POST'])
def register():
    regis_form  = RegistrationForm(request.form, prefix='register-form')
    if regis_form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(regis_form.password_regis.data).decode('utf-8')
        user = User(username=regis_form.username_regis.data, email=regis_form.email_regis.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in', 'success')
        return redirect(url_for('home'))
    return redirect(url_for('home'))

Below is the home.html file:

<div class="" id="login">
                  <form method="POST" action="{{ url_for('login') }}">
                      {{ login_form.hidden_tag() }}
                      <fieldset class="form-group">
                          <div class="form-group">
                              {% if login_form.email_login.errors %}
                                  {{ login_form.email_login(class="form-control form-control-md is-invalid") }}
                                  <div class="invalid-feedback">
                                      {% for error in login_form.email_login.errors %}
                                          <span>{{ error }}</span>
                                      {% endfor %}
                                  </div>
                              {% else %}
                                  {{ login_form.email_login(class="form-control form-control-md mb-2", placeholder="Email") }}
                              {% endif %}
                          </div>
                          <div class="form-group">
                              {% if login_form.password_login.errors %}
                                  {{ login_form.password_login(class="form-control form-control-md is-invalid") }}
                                  <div class="invalid-feedback">
                                      {% for error in login_form.password_login.errors %}
                                          <span>{{ error }}</span>
                                      {% endfor %}
                                  </div>
                              {% else %}
                                  {{ login_form.password_login(class="form-control form-control-md mb-2", placeholder="Password") }}
                              {% endif %}
                          </div>
                          <div class="form-check">
                              {{ login_form.remember(class="form-check-input") }}
                              {{ login_form.remember.label(class="form-check-label") }}
                          </div>
                      </fieldset>
                      <div class="form-group">
                          {{ login_form.submit_login(class="btn btn-danger") }}
                          <small class="text-muted ml-2">
                              <a href="">Forgot Password?</a>
                          </small>
                      </div>
                  </form>
              </div>
              <div class="" id="regis">
                  <form method="POST" action="{{ url_for('register') }}">
                      {{ regis_form.hidden_tag() }}
                      <fieldset class="form-group">
                          <div class="form-group">
                              {% if regis_form.username_regis.errors %}
                                  {{ regis_form.username_regis(class="form-control form-control-md is-invalid") }}
                                  <div class="invalid-feedback">
                                      {% for error in regis_form.username_regis.errors %}
                                          <span>{{ error }}</span>
                                      {% endfor %}
                                  </div>
                              {% else %}
                                  {{ regis_form.username_regis(class="form-control form-control-md mb-2", placeholder="Username") }}
                              {% endif %}
                          </div>
                          <div class="form-group">
                              {% if regis_form.email_regis.errors %}
                                  {{ regis_form.email_regis(class="form-control form-control-md is-invalid") }}
                                  <div class="invalid-feedback">
                                      {% for error in regis_form.email_regis.errors %}
                                          <span>{{ error }}</span>
                                      {% endfor %}
                                  </div>
                              {% else %}
                                  {{ regis_form.email_regis(class="form-control form-control-md mb-2", placeholder="Email") }}
                              {% endif %}
                          </div>
                          <div class="form-group">
                              {% if regis_form.password_regis.errors %}
                                  {{ regis_form.password_regis(class="form-control form-control-md is-invalid") }}
                                  <div class="invalid-feedback">
                                      {% for error in regis_form.password_regis.errors %}
                                          <span>{{ error }}</span>
                                      {% endfor %}
                                  </div>
                              {% else %}
                                  {{ regis_form.password_regis(class="form-control form-control-md mb-2", placeholder="Password") }}
                              {% endif %}
                          </div>
                      </fieldset>
                      <div class="form-group">
                          {{ regis_form.submit_regis(class="btn btn-danger") }}
                      </div>
                  </form>
              </div>

We (the support team on these forums) can only really help with PythonAnywhere questions, not general Python programming problems. Perhaps someone else here could help, but you might have more luck asking on a general programming Q&A site like Stack Overflow.