I am trying to perform a WTForm error check. I found this snippet and modified it a bit:
def flash_errors(form): """Flashes form errors""" for field, errors in form.errors.items(): for error in errors: flash(u"Error in the %s field - %s" % ( getattr(form, field).label.text, error ), 'error')
Here is one of my form classes:
class ContactForm(Form): """Contact form"""
And view:
@app.route("/contact/", methods=("GET", "POST")) def contact(): """Contact view""" form = ContactForm() flash_errors(form) if form.validate_on_submit(): sender = "%s <%s>" % (form.name.data, form.email.data) subject = "Message from %s" % form.name.data message = form.message.data body = render_template('emails/contact.html', sender=sender, message=message) email_admin(subject, body) flash("Your message has been sent. Thank you!", "success") return render_template("contact.html", form=form)
However, validation errors are not displayed. I know that my forms and templates work fine, because my success message flashes when the data is valid. What's wrong?
source share