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()])
Is there anyway to specify a validator such that either email
or phone
is required?
You can create a
validate
method on the form and do some manual checking. Something like this might get you started.Thank you @reptilicus. I had minor changes to the answer for it to work.
self.email.data and self.phone.data
validate()
method instead ofelse
condition.