Why error messages doesn't show up in form validation in Flask?

Submitted 2 years, 9 months ago
Ticket #359
Views 448
Language/Framework Python
Priority Medium
Status In-Progress

When I fill the form with data correctly, it creates the account and redirects to the login page - just as it should work. But when I will do something against the validators eg. try to create a username with only one character - then an account won't be created (which is good), but also any error message won't appear. After many hours of experiments and changing the code, I still don't know what's causing that problem, especially when I used to create things like that using the same method and everything worked perfectly. If you need more information/code please tell me and I'll edit the post.

Registration Form:

class RegistrationForm(FlaskForm):
    username = StringField('Name',
                           validators=[DataRequired(), Length(min=2, max=20)],
                           render_kw={"placeholder": "Username"})
    email = StringField('Email',
                        validators=[DataRequired(), Email()],
                        render_kw={"placeholder": "Email"})
    password = PasswordField('Password',
                             render_kw={"placeholder": "Password"})
    confirm_password = PasswordField('Confirm Password',
                                     validators=[DataRequired(), EqualTo('password')],
                                     render_kw={"placeholder": "Confirm Password"})  # equal to password field
    submit = SubmitField('Sign Up')


<section class="banner-b">

    <div class="registration-box">
        <form method="POST">
            {{ form.hidden_tag() }}
            <div class="textbox">
                {% if form.username.errors %}
                    <div class="invalid-feedback">
                        {% for error in form.username.errors %}
                            <span>{{ error }}</span>
                        {% endfor %}
                {% else %}
                    <i class="fa fa-user" aria-hidden="true"></i>
                    {{ form.username(class="text") }}
                {% endif %}
     [... other forms for email, password etc...]

Submitted on Feb 23, 21

are you using WTForms? If not please provide the entire forms.py details just would like to know what kind of forms you are using. - Vengat 2 years, 9 months ago

Vicif95865, can you please provide the details about WTForms, so that our SME's can come up with the better solution. - revathirv 2 years, 9 months ago
add a comment

1 Answer

I am not familiar with Flask as I am Django fan, may be try this,

for fieldName, errorMessages in form.errors.items():
    for err in errorMessages:
        # do something with your errorMessages for fieldName

Submitted 2 years, 9 months ago

Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message

Latest Blogs