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 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

Benjamin,

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,631 Points

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