Phoenix
Phoenix

Reputation: 399

WTForm validator not giving feedback on non-numeric values

I have a wtform and want to give feedback to users if they enter a non numeric value. I am using

availablequantity = IntegerField('Available quantity',
                    validators=[ NumberRange(min=1, max=100), DataRequired()])

and on html side I have,

<div class="form-group">
      {% if form.availablequantity.errors %}
          {{ form.availablequantity(class="form-control is-invalid") }}
          <div class="invalid-feedback">
              {% for error in form.availablequantity.errors %}
                  <span>{{ error }}</span>
              {% endfor %}
          </div>
      {% else %}
          {{ form.availablequantity(class="form-control", placeholder="Available quantity") }}
      {% endif %}
</div>

now if user enters a numeric value things are fine but if they enter non-numeric values, I don't get any useful feedback (I was expecting something like, "you must enter a numeric value between 1 and 100")

Upvotes: 1

Views: 36

Answers (1)

Add the following in your request (not in the form class:

form.availablequantity.errors.append("Enter num between 1 and 100")

or add message argument

 NumberRange(min=1, max=100, message='Num must be between 1 and 100')

Upvotes: 1

Related Questions