this is my code for password_check and everything is working great but i don’t fully understand why i need to put second parameter inside brackets def password_check(form,field): in order my function to work. when i remove field from brackets it doesn’t work anymore
here is my code
import re from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, PasswordField from wtforms.validators import EqualTo, InputRequired, Email, ValidationError from models import User def password_check(form,field): password = form.password.data if len(password)< 4: raise ValidationError('Password must be at lest 8 letters long') elif re.search('[0-9]',password) is None: raise ValidationError('Password must contain a number') elif re.search('[A-Z]',password) is None: raise ValidationError('Password must have one uppercase letter') class RegistrationForm(FlaskForm): name = StringField(validators=[InputRequired(message='Please enter Your name')]) lastname = StringField(validators=[InputRequired(message='Please enter Your lastname')]) email = StringField(validators=[InputRequired(), Email()]) password = PasswordField(validators=[InputRequired(),password_check]) confirm_pass = PasswordField( validators=[InputRequired(),EqualTo('password', message='Passwords must match')]) submit = SubmitField('Register')
validators argument to
StringField() expects some sort of sequence (in your case you’re passing in a
list, but I suspect anything iterable should work)
From the docs
validators – A sequence of validators to call when validate is called.
field are required arguments to your validator simply because they’re passed to each validator .. per the Custom validators docs (emphasis mine)
All we’ve done here is move the exact same code out of the class and as a function. Since a validator can be any callable which accepts the two positional arguments form and field, this is perfectly fine, but the validator is very special-cased.
Answered By – ti7
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0