1

I am unable to figure out why my form will not validate. I have made sure that the CSRF field is inserted in to the HTML with form.hidden_tag

Here is the code for my registration form:

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


class RegistrationForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=2, max=20)])
    email = StringField('Email', validators=[DataRequired(), Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo(password)])
    submit = SubmitField('Register')

Here is the function in which I am checking to see if the form is validated. It does submit, as the program prints out 'submitted' into my terminal

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegistrationForm()
    if form.is_submitted():
        print("submitted")
    if form.validate_on_submit():
        flash(f'Account created for {form.username.data}!', 'success')
        return redirect(url_for('home'))
    print(forms.errors)
    return render_template('register.html', title='Register', form=form)

Using forms.errors, I get the following error message:

{'confirm_password': ["Invalid field name '<UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.DataRequired object at 0x00000205913D2F28>]})>'."]}

I am unsure of what this means.

If it helps, I have all my current code located on GitHub: https://github.com/tomajohnson21/FakeBook

2 Answers 2

9

You are using the class member password instead of the "fieldname", thus you get the error.

Incorrect:

EqualTo(password)

Correct:

EqualTo('password')

There is an example of EqualTo in WTForms validators documentation

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

Comments

1

You can only put single quotes to password to solve it.

confirm_password = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password')])

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.