gpt4 book ai didi

python - WTForm "OR"条件验证器? (电子邮件或电话)

转载 作者:行者123 更新时间:2023-11-28 22:35:04 29 4
gpt4 key购买 nike

class ContactForm(Form):
name = StringField('Name',
validators=[DataRequired(), Length(max=255)])
email = StringField('Email',
validators=[Optional(), Email(), Length(max=255)])
phone = StringField('Phone number',
validators=[Optional(), NumberRange(min=8, max=14)])
comment = TextAreaField(u'Comment',
validators=[DataRequired()])

有没有办法指定一个验证器,使得 emailphone 是必需的?

最佳答案

您可以在表单上创建一个validate 方法并进行一些手动检查。像这样的东西可能会让你开始。

class MyForm(Form):
name = StringField('Name',
validators=[DataRequired(), Length(max=255)])
email = StringField('Email',
validators=[Optional(), Email(), Length(max=255)])
phone = StringField('Phone number',
validators=[Optional(), NumberRange(min=8, max=14)])
comment = TextAreaField(u'Comment',
validators=[DataRequired()])
def validate(self):
valid = True
if not Form.validate(self):
valid = False
if not self.email and not self.phone:
self.email.errors.append("Email or phone required")
valid = False
else:
return valid

关于python - WTForm "OR"条件验证器? (电子邮件或电话),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38486527/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com