Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Wtforms: Validation error message for SelectField

I have a Selectfield with some options and a blank default option with a custom validator

class Questions(FlaskForm):
     question = SelectField('What"s up?', choices=[(None,"Please select an option"),(1,"Good"),(2,"Bad")],validators=[Required(message="Please select an option")])
     def custom_validator(self,question):
         if question == "None":
              self.question.errors += (ValidationError("Please select an option"))

In my routes I call :

if form.validate_on_submit():
    #do something
else:
    #so something else

and my html looks like this

{{form.question.label}}
{{form.question.label}}

The custom validator seems to fire but the error message is not displayed as they normally are, e.g. when using a Required() validator on a StringField.

Am I missing something? Is there any good way of getting the tooltip error message for SelectFields?

Thanks.

question from:https://stackoverflow.com/questions/65918531/wtforms-validation-error-message-for-selectfield

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...