liveBook · Manning

Validation

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Validation is the process of checking input data against required presence, format, and value constraints.

Validation checks submitted input before the application accepts or processes it. A login form, for example, must handle missing values, length violations, and malformed email addresses rather than trusting user input. section 8.2.2

A practical validation routine can check:

  • Whether required fields are present. section 8.2.2
  • Whether values fit length restrictions and standardized formats. section 8.2.2
  • Whether a value satisfies an application-specific rule, such as an email address not already existing in the database. section 8.4.2
from flask_wtf import FlaskForm                         #1
from wtforms import PasswordField,                      #2
BooleanField, SubmitField                             #2
from wtforms.fields.html5 import EmailField             #2
from wtforms.validators import DataRequired,            #3
Length, Email, EqualTo                                #3
 
class LoginForm(FlaskForm):                             #4
    email = EmailField(                                 #5
        "Email",                                        #5
        validators=[DataRequired(), Length(             #5
            min=4,                                      #5
            max=128,                                    #5
            message="Email must be between 4            #5
            and 128 characters long"                  #5
        ), Email()],                                    #5
        render_kw={"placeholder": " "}                  #5
    )                                                   #5
    password = PasswordField(                           #6
        "Password",                                     #6
        validators=[DataRequired(), Length(             #6
            min=3,                                      #6
            max=64,                                     #6
            message="Password must be between 3         #6
            and 64 characters long"                   #6
        )],                                             #6
        render_kw={"placeholder": “ “}                  #6
    )                                                   #6
    remember_me = BooleanField(" Keep me logged in")    #7
cancel = SubmitField(                                   #8
    label="Cancel",                                     #8
    render_kw={"formnovalidate": True},                 #8
    )                                                   #8
    submit = SubmitField("Log In")                      #9

A Flask-WTF form binds server-side fields to HTML elements and assigns reusable validators to each field. section 8.2.2

Form libraries centralize these checks and expose the result through form validation. Custom validators can query application data and raise a validation error, while templates display field-specific messages. Handlers then proceed only when validation succeeds; otherwise they return feedback instead of creating invalid records. section 8.4.2 section 11.8.3

Validating a comment submission

A comment handler creates a form, calls validate_on_submit(), and saves the stripped comment only when validation passes. If validation fails, it flashes a message and redirects without creating a post. section 11.8.3

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains field validators, custom database-backed checks, error display, and conditional persistence without cataloging broader validation strategies.
Examples
Uses login, new-user registration, duplicate-email detection, and hierarchical comment creation as concrete validation scenarios.
What it assumes
Assumes familiarity with HTML forms, HTTP POST handlers, templates, and database queries while teaching constraint checks through application examples.
What you can do after
Apply it via Flask-WTF validators and validate_on_submit() before rendering errors or committing accepted records.
How it connects
Connects HTML forms, HTTP POST requests, Flask-WTF, WTForms, authentication, templates, and database persistence.

Also explained in

Compare how 30 books explain validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.