Bird
Raised Fist0
Flaskframework~10 mins

Flask-WTF for form validation - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the FlaskForm base class from Flask-WTF.

Flask
from flask_wtf import [1]
Drag options to blanks, or click blank then click option'
ABaseForm
BFlaskForm
CFormBase
DForm
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Form' instead of 'FlaskForm' causes import errors.
Trying to import 'FormBase' which does not exist in Flask-WTF.
2fill in blank
medium

Complete the code to define a text input field named 'username' using WTForms.

Flask
from wtforms import StringField

class LoginForm(FlaskForm):
    username = [1]('Username')
Drag options to blanks, or click blank then click option'
ATextField
BCharField
CInputField
DStringField
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'TextField' which is deprecated in WTForms.
Using 'CharField' which is not part of WTForms.
3fill in blank
hard

Fix the error in the code to add a required validator to the email field.

Flask
from wtforms.validators import DataRequired, Email

class ContactForm(FlaskForm):
    email = StringField('Email', validators=[[1]])
Drag options to blanks, or click blank then click option'
ADataRequired()
BRequired()
CEmail()
DInputRequired()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Required()' which is deprecated.
Using 'Email()' which only checks email format, not presence.
4fill in blank
hard

Fill both blanks to create a password field with a length validator requiring 6 to 12 characters.

Flask
from wtforms import PasswordField
from wtforms.validators import Length

class RegisterForm(FlaskForm):
    password = PasswordField('Password', validators=[Length(min=[1], max=[2])])
Drag options to blanks, or click blank then click option'
A6
B12
C8
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping min and max values.
Using values outside the required range.
5fill in blank
hard

Fill all three blanks to create a form with a submit button labeled 'Send', a CSRF token, and a text area field named 'message'.

Flask
from flask_wtf import FlaskForm
from wtforms import SubmitField, TextAreaField

class MessageForm(FlaskForm):
    message = [1]('Message')
    submit = [2]('Send')

# CSRF protection is enabled by default with [3]
Drag options to blanks, or click blank then click option'
ATextAreaField
BSubmitField
CFlaskForm
DStringField
Attempts:
3 left
💡 Hint
Common Mistakes
Using StringField instead of TextAreaField for message.
Forgetting that CSRF is enabled by FlaskForm.

Practice

(1/5)
1. What is the main purpose of using Flask-WTF in a Flask application?
easy
A. To simplify form creation and add validation easily
B. To create database models automatically
C. To handle user authentication and sessions
D. To serve static files like images and CSS

Solution

  1. Step 1: Understand Flask-WTF role

    Flask-WTF is a Flask extension that helps create forms and validate user input easily and securely.
  2. Step 2: Compare options with Flask-WTF purpose

    Options A, B, and D relate to other Flask features or extensions, not form validation.
  3. Final Answer:

    To simplify form creation and add validation easily -> Option A
  4. Quick Check:

    Flask-WTF = form handling and validation [OK]
Hint: Flask-WTF = forms + validation helper [OK]
Common Mistakes:
  • Confusing Flask-WTF with database or auth tools
  • Thinking Flask-WTF serves static files
  • Assuming Flask-WTF manages sessions
2. Which of the following is the correct way to import the FlaskForm base class from Flask-WTF?
easy
A. from flask_wtf import FlaskForm
B. from flask import FlaskForm
C. import FlaskForm from flask_wtf
D. from flask_wtf.forms import FlaskForm

Solution

  1. Step 1: Recall Flask-WTF import syntax

    The FlaskForm class is imported directly from the flask_wtf package using 'from flask_wtf import FlaskForm'.
  2. Step 2: Check other options for syntax errors or wrong paths

    from flask import FlaskForm imports from flask (wrong package). import FlaskForm from flask_wtf uses invalid syntax. from flask_wtf.forms import FlaskForm imports from a non-existent submodule.
  3. Final Answer:

    from flask_wtf import FlaskForm -> Option A
  4. Quick Check:

    Correct import = from flask_wtf import FlaskForm [OK]
Hint: FlaskForm is from flask_wtf, not flask [OK]
Common Mistakes:
  • Importing FlaskForm from flask instead of flask_wtf
  • Using incorrect import syntax like 'import X from Y'
  • Assuming FlaskForm is in a submodule
3. Given this Flask-WTF form class, what will form.validate_on_submit() return if the submitted email is empty?
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired, Email

class EmailForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Email()])
medium
A. Raises an exception due to missing email
B. True, because the form is submitted
C. True, because Email validator ignores empty fields
D. False, because the email field is empty and required

Solution

  1. Step 1: Understand validators on email field

    The email field uses DataRequired() which means it must not be empty, and Email() to check format.
  2. Step 2: Analyze validate_on_submit() behavior with empty email

    Since email is empty, DataRequired() fails, so validate_on_submit() returns False.
  3. Final Answer:

    False, because the email field is empty and required -> Option D
  4. Quick Check:

    Empty required field = validation fails [OK]
Hint: Empty required fields cause validation to fail [OK]
Common Mistakes:
  • Assuming empty fields pass Email validator
  • Thinking form submission means validation is True
  • Expecting exceptions instead of False return
4. Identify the error in this Flask-WTF form definition:
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import Length

class NameForm(FlaskForm):
    name = StringField('Name', validators=Length(min=3, max=25))
medium
A. FlaskForm must be imported from wtforms, not flask_wtf
B. StringField does not accept validators argument
C. validators should be a list, not a single Length instance
D. Length validator requires a message argument

Solution

  1. Step 1: Check validators argument type

    Validators must be passed as a list or tuple, even if only one validator is used.
  2. Step 2: Identify the error in code

    Here, validators=Length(...) passes a single instance, not a list, causing a TypeError.
  3. Final Answer:

    validators should be a list, not a single Length instance -> Option C
  4. Quick Check:

    Validators must be in a list [OK]
Hint: Always wrap validators in a list [] [OK]
Common Mistakes:
  • Passing a single validator without list brackets
  • Thinking validators argument is optional
  • Importing FlaskForm from wrong package
5. You want to create a Flask-WTF form that requires a username between 4 and 12 characters and an optional email that must be valid if provided. Which validator setup is correct?
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired, Length, Email, Optional

class UserForm(FlaskForm):
    username = StringField('Username', validators=[DataRequired(), Length(min=4, max=12)])
    email = StringField('Email', validators=[???])
hard
A. [DataRequired(), Email()]
B. [Optional(), Email()]
C. [Email(), Optional()]
D. [Length(min=4, max=12), Optional()]

Solution

  1. Step 1: Understand optional email validation

    Email is optional, so Optional() must come before Email() to allow empty input without error.
  2. Step 2: Check validator order and correctness

    [Optional(), Email()] correctly uses [Optional(), Email()] so empty email passes, and if present, Email() validates format.
  3. Final Answer:

    [Optional(), Email()] -> Option B
  4. Quick Check:

    Optional before Email for optional valid email [OK]
Hint: Put Optional() before Email() for optional email fields [OK]
Common Mistakes:
  • Using DataRequired() on optional fields
  • Reversing order of Optional() and Email()
  • Using Length validator on email field
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard