how to view WTForms validation errors?
Asked Answered
H

3

8

I am writing some basic tests and have a test failing.

def test_new_user_registration(self):
  self.client.get('/user/register')
  form = RegistrationForm(
    email=u'[email protected]',
    first_name=u'Alex',
    last_name=u'Frazer',
    username=u'crow',
    password=u'fake_password',
    confirm_password=u'fake_password'
  )
  self.assertTrue(form.validate())

The assertion error is failing on form.validate(), but how can I view what the validation errors are?

Heterography answered 4/7, 2014 at 16:12 Comment(0)
R
9

Use form.errors:

errors

A dict containing a list of errors for each field. Empty if the form hasn’t been validated, or there were no errors.

Romo answered 4/7, 2014 at 16:16 Comment(0)
C
3

You can print the errors by adding the following: print form.errors.items().

Covenanter answered 12/5, 2018 at 19:26 Comment(0)
T
2

I had the same problem, and couldnt find the answer anywhere. This is a snippet from my index.html

 <div class="all">
      <div class="form-group">
        {{ form.phone.label(id="label1") }}
        {% if form.phone.errors %}
        {{ form.phone(class="form-control form-control-lg is-invalid") }}
        <div class="invalid-feedback">
            {% for error in form.phone.errors %}
                <span>{{ error }}</span>
            {% endfor %}
        </div>
    {% else %}
    {{ form.phone(class="form-control", placeholder="Enter Your Phone Number", )}}
    {% endif %}
       

      </div>
    </div>

And in my forms.py

class AddContestant(FlaskForm):
number = StringField('Phone number', validators=[DataRequired(), Length(min=10, max=15, message="Your phone number should be more than 10 digits and less than 15")])

enter image description here

Tentacle answered 17/4, 2022 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.