WTForms getting the errors WTForms getting the errors python python

WTForms getting the errors


The actual form object has an errors attribute that contains the field names and their errors in a dictionary. So you could do:

for fieldName, errorMessages in form.errors.items():    for err in errorMessages:        # do something with your errorMessages for fieldName


A cleaner solution for Flask templates:

Python 3:

{% for field, errors in form.errors.items() %}<div class="alert alert-error">    {{ form[field].label }}: {{ ', '.join(errors) }}</div>{% endfor %}

Python 2:

{% for field, errors in form.errors.iteritems() %}<div class="alert alert-error">    {{ form[field].label }}: {{ ', '.join(errors) }}</div>{% endfor %}


For anyone looking to do this in Flask templates:

{% for field in form.errors %}{% for error in form.errors[field] %}    <div class="alert alert-error">        <strong>Error!</strong> {{error}}    </div>{% endfor %}{% endfor %}