'how to view WTForms validation errors?

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alecxe
Solution 2
Solution 3 Nana Kweku Adumatta