0

I have tested my code by typing some wrong thing, but once I click submit, no error message was generated on the screen

I also tried to type the right things to try but once I checked with print, it shows that the validation is always failed. (solved)

app:

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        print('validated')
        username = form.username.data
        password = bcrypt.generate_password_hash(form.password.data)
        email = form.mail.data
        print(username, password, email)
        flash("Successfully registered")
    else:
        print('validation failed')
    return render_template('register.html', form=form)

RegisterForm:

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Length, EqualTo, Email

class RegisterForm(FlaskForm):
    username = StringField('Username', [validators.Length(min=4, max=25)])
    password = PasswordField('Password', validators=[DataRequired(), Length(min=6, max=20)])
    confirm_password = PasswordField('Re-enter Password', validators=[DataRequired(), EqualTo(password)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    submit = SubmitField('Register')

html:

{% extends 'base.html' %}
{% import "bootstrap/wtf.html" as wtf %}

{% block content %}
    <div class = 'container'>
        <br>
        <h1>Register Now</h1>
        <br>
        <div class="row">
            <div class="col-md-8">
                {{ form.hidden_tag() }}
                {{ wtf.form_errors(form, hiddens="only") }}

                {{ wtf.form_field(form.username) }}
                {{ wtf.form_field(form.password) }}
                {{ wtf.form_field(form.confirm_password) }}
                {{ wtf.form_field(form.email) }}
                {{ wtf.form_field(form.submit) }}
            </div>
        </div>
    </div>
{% endblock %}

I have also tried

{{ wtf.quick_form(form) }}

But which does not generate the error message either.

Suckway
  • 11
  • 5

1 Answers1

0

Have you tried just:

{{ wtf.form_errors(form) }}

According to the docs:

hiddens – If True, render errors of hidden fields as well. If 'only', render only these.

So you're only showing errors for hidden fields.

djnz
  • 2,021
  • 1
  • 13
  • 13