Validation for FlaskWTForm fields

Report a typo

We have a Flask form class with the following code.

We have the following fields

  1. name: String field, used to accept name inputs from the user
  2. age: Integer field, used to accept age input as an integer from the user

We need the following validations on the fields:

  • name: data is required to be always present
  • age: data is required to be always present, between 12 to 20.
Write a program in Python 3
from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField
from wtforms.validators import InputRequired, EqualTo, DataRequired, Length, ValidationError, NumberRange

# Form
class StudentForm(FlaskForm):
name = StringField("name")
age = IntegerField("age")

Create a free account to access the full topic