Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

flask - Validation based on other wtforms.validators in the same form? (i.e. URL or Email based on checkbox)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...