请解释 Flask-WTF

Explanation for Flask-WTF please

这是我的 password_check 代码,一切正常,但我不完全理解为什么我需要将第二个参数放在方括号 def password_check(form,field): 中我的工作职能。当我从括号中删除字段时,它不再起作用 这是我的代码

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

StringField()validators 参数需要某种 sequence (在你的情况下你传递的是 list,但我怀疑任何可迭代的东西都应该工作)

来自docs

validators – A sequence of validators to call when validate is called.

formfield 是验证器的必需参数,因为它们已传递给每个验证器.. 根据 Custom validators docs(强调我的)

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.