Overview
The Document Download Frontend uses Flask-WTF and WTForms for form handling and validation. Form validation leverages thenotifications-utils library for consistent email validation across GOV.UK Notify services.
File: app/forms.py
Email Address Form
EmailAddressForm Class
Purpose: Validate email addresses before allowing document downloads Inherits:FlaskForm (aliased as Form)
Usage: Email confirmation page (/confirm-email-address)
email_address
- Type:
EmailAddressField(custom field) - Label:
"Email address" - Validators:
DataRequired("Enter your email address")- Ensures field not emptyValidEmail()- Custom email validation
- Filters:
strip_all_whitespace- Removes all whitespace from input
Form Usage in Views
Route:confirm_email_address() (index.py:102)
- Form instantiated on GET request
- User submits email address
validate_on_submit()checks CSRF token and runs validators- If valid, authenticate with backend API
- On auth failure, append custom error to
form.form_errors
Custom Field: EmailAddressField
Field Definition
- Error handling: Passes first error to
errorMessageparameter - Input type: Uses
type="email"for browser validation hints - Autocomplete: Sets
autocomplete="email"for browser autofill - Spellcheck: Disables spellcheck (inappropriate for email addresses)
- GOV.UK component: Renders via
govuk_input.htmltemplate
Custom Validator: ValidEmail
Validator Class
- Skip validation if field empty (handled by
DataRequired) - Call
validate_email_address()fromnotifications-utils - Raise WTForms
ValidationErrorif email invalid
- Valid email format (RFC 5322)
- No consecutive dots
- Valid TLD
- Maximum length checks
- Character restrictions
Error Messages
Default validator error:Form Filters
strip_all_whitespace
Source:notifications_utils.formatters
Purpose: Remove all whitespace characters from email input
Applied to: email_address field
Example:
CSRF Protection
CSRF Token
Flask-WTF automatically adds CSRF protection to all forms. In template:validate_on_submit()
Error handling: CSRF errors handled by Flask-WTF error handler
Form Error Handling
Field Errors
Validator errors attached to specific fields:Form-Level Errors
Custom errors not tied to specific fields:Status Codes
Form display (GET):200- Form rendered successfully
200- Validation passed, redirecting to download400- Validation errors (re-render form)429- Rate limit exceeded (too many authentication attempts)
Integration with GOV.UK Design System
Error Summary Component
Displays all errors at top of page:Input Component
Email field rendered with GOV.UK input component:Testing Considerations
Valid Email Examples
Whitespace Handling
Dependencies
Required packages:Flask-WTF- Flask integration for WTFormsWTForms- Form librarynotifications-utils- Notify-specific validation and formattingMarkupSafe- Safe HTML rendering for error messages