2

I'm trying to setup a flask webapp with some basic login/register system controlled by flask-WTF forms.

Here is my code:

html

<!-- Register form -->
<div class="form">
    <div class="form-area">
        <h2>Register</h2>
        <form action="{{ url_for('register') }}">
            {{ form.csrf_token() }}
            {{ form.hidden_tag() }}
            {{ form.name(placeholder='name') }}
            {{ form.surname(placeholder='surname') }}
            {{ form.email(placeholder='email') }}
            {{ form.password(placeholder='password') }}
            {{ form.confirm_password(placeholder='confirm password') }}
            <input type="submit" value="Register">
        </form>
        <p>Already registered?<a href="{{ url_for('login') }}"> Log in here</a></p>
    </div>
    <div class="error-area">
        {% for error in form.confirm_password.errors %}
        <p>{{ error }}</p>
        {% endfor %}
    </div>
</div>

class

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import InputRequired, Length, EqualTo

class RegisterForm(FlaskForm):
    name = StringField('name', validators=[InputRequired()])
    surname = StringField('surname', validators=[InputRequired()])
    email = StringField('email', validators=[InputRequired()])
    password = PasswordField('password', validators=[InputRequired(), Length(min=6)])
    confirm_password = PasswordField('confirm passord', validators=[InputRequired(), Length(min=6), EqualTo(password)])

flask

@app.route('/register')
def register():

    #declare a register form
    form = RegisterForm()

    #validate form
    if form.validate_on_submit():
        print('validate')
        return '<h1>Success</h1>'
    else:
        print('not validated')
        print(form.errors)

    return render_template('register.html', form=form)

The problem with my code is that validation seems not to be working. Even if I fill the form with the "valid" input, form.validate_on_submit() always fail. What I can't understand is that even when I try to print array errors, no error shows.

What am I missing?

1 Answer 1

1

There are a few issues here. Firstly, in your html, you haven't set a method attribute for the form. This means that it defaults to GET, which is why the form isn't validating. This can be changed like so:

<form action="{{ url_for('register') }}" method='POST'>

Incidentally, as the view that loads the form is the same as the target, you can leave out the action attribute, giving us:

<form method='POST'>

Secondly, in your class, you have a couple of issues with the confirm_password field. Firstly, you have a typo in PasswordField('confirm passord'. Secondly, the EqualTo() validator expects a string, not a field. We need to change this line in full to:

confirm_password = PasswordField('confirm password', validators=[InputRequired(), Length(min=6), EqualTo('password')])

Finally, in your flask, we need to accept POST requests to the view. This can be done by altering @app.route():

@app.route('/register', methods=['POST', 'GET'])

Having made these changes, the form should work as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.