Hope this is not too complicated, but I get the feeling I may need to apply validation outside of wtforms here...
I have an StringField that I want to validate as either Email or URL based on a checkbox the user would click. Ideally I want to add and show "mailto:" in the actual form field before sending it to POST as well.
Could I accomplish this with a validation method? Or are there better ways to go about this? TIA!
from wtforms import StringField, SubmitField, BooleanField
from wtforms.validators import DataRequired, ValidationError, Email, URL
class LinkForm(FlaskForm):
name = StringField(u'Page Name', validators=[DataRequired()])
url = StringField(u'Address (URL or Email)', validators=[DataRequired(), URL()])
mailto = BooleanField(u'This is an email address') #checkbox
submit = SubmitField(u'Submit')
def validate_url(self, url):
link = Link.query.filter_by(url=url.data).first(
(now validate_url method just checks for an entry w/ same url)
question from:
https://stackoverflow.com/questions/65913466/validation-based-on-other-wtforms-validators-in-the-same-form-i-e-url-or-emai 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…