Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask Takin' Names Macros

Validation

Validation works; however, messages don't show. By messages I mean "This field is required invalid email"... etc. Here is my forms code. Btw im doing it in Pycharm :)

from flask_wtf import Form
from wtforms import StringField,PasswordField
from wtforms.validators import DataRequired,Regexp,ValidationError, Email,Length,EqualTo

from models import User


def name_exists(form,field):
    if User.select().where(User.username==field.data).exists():
        raise ValidationError('User with that name already exists.')


def email_exists(form,field):
    if User.select().where(User.email==field.data).exists():
        raise ValidationError('User with that email already exists.')

class RegisterForm(Form):
    username = StringField(
        'Username',
        validators=[
            DataRequired(),
            Regexp(
                r'^[a-zA-Z0-9_]+$',
                message= ("Username should be one word, "
                          "numbers, and underscores only.")
            ),
            name_exists
        ])
    email= StringField(
        'Email',
        validators=[
            DataRequired(),
            Email(),
            email_exists

        ])
    password = PasswordField(
        'Password',
        validators=[
            DataRequired(),
            Length(min=6),
            EqualTo('password2',message="Passwords must match")

        ])
    password2 = PasswordField(
        'Confirm Password',
        validators=[DataRequired()]
    )

[MOD: added ```python formatting -cf]

2 Answers

David Smith
David Smith
10,577 Points

bump,

I have the same problem, the only validator that seems to work is the DataRequired.