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

Ayman Said
seal-mask
.a{fill-rule:evenodd;}techdegree
Ayman Said
Python Web Development Techdegree Student 14,717 Points

How the custom validators work in terms of calling and passed parameters

In the Flask-WTF video we added a

name_exists

the validator in the username validators list, we've added it like an object and certainly not as a function call,

  1. how such a thing would call the defined function
def name_exists(form, field)

and does it expect that the function would have these two: form & field parameters?

  1. name_exists(form, field) parameters usage inside the function is not clear where they come from? and how Python knows that field is connected to the form?!

Appreciate your help, thanks in advance.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

In wtfforms, Stringfield is a constructor that returns a form field. The parameter validators lists a "sequence of validators to call when validate is called."

"*A validator simply takes an input, verifies it fulfills some criterion, such as a maximum length for a string and returns. Or, if the validation fails, raises a ValidationError. This system is very simple and flexible, and allows you to chain any number of validators on fields.

Under custom validators it states "...a validator can be any callable which accepts the two positional arguments form and field..."

When Stringfield is constructed, during it's __init__ execution, each validator is added to a list of functions to be run when ever the form field need to be verified correct. The form is set to the current form, and the field is set to the current field (the Stringfield in this case).

Post back if you need more help. Good luck!!

Ayman Said
seal-mask
.a{fill-rule:evenodd;}techdegree
Ayman Said
Python Web Development Techdegree Student 14,717 Points

You hit again Mr. Chris Freeman , thanks. Your answer is clear. Is there's an internal implementation of wtfforms where it assumes for any custom validator found in the validators chain, there's a function holding the same exact name with (form, field) parameters?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Note that the parameters form and field are positional. This means that their actual name in the validator function is irrelevant, just as long as there are two that can received the form and field passed in.

The function listed in the validators list must exist as an object or a NameError will be raised.

As for testing the validator, the testing might be at instantiation of the field, or it might be at form POST time. You can test this by adding a validator that does exist by does not have two positional parameters. If the check is done at instantiation the code form will not populate. Otherwise the check my just be to try it when the form gets posted.

If you test it out, please report back on what you find!!