Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Build a Social Network with Flask Takin' Names Flask-WTF Forms

Benjamin Orimoloye
Benjamin Orimoloye
23,328 Points

Why the "form" parameter? It was never used in the function

In the function def username_exists(form, field):, field was used, but form wasn't. Why isn't there?

2 Answers

Nicolas Hampton
Nicolas Hampton
44,626 Points


The best answer to this is looking at the source code for the "EqualTo" validator: https://github.com/wtforms/wtforms/blob/283b2803206825158834f1828bbf749c129b7c47/src/wtforms/validators.py#L88

The "form" reference that's passed into validators makes it possible to validate based on the value of other fields as well. The perfect example of this is a password and confirm password field, where the validity of one field is dependent on another.

Two things about this source code: the validator is a class, not a function. Don't let this throw you. The call method is basically the same as the function validators we're writing in this class. They're creating a class here to allow you to pass arguments to the init method to customize the validator method.

Another student asked on the forums why EqualTo takes a string instead of a reference to password2, and here you can see why. Internally, EqualTo is using that string as a key for the form parameter!

Hope this helps!

J llama
J llama
12,629 Points

Kenneth does a pretty bad job of explaining this one here, i have the same question