FormEncode

2.1.1 · active · verified Thu Apr 16

FormEncode is a Python library for HTML form validation, generation, and data conversion. It handles complex, nested data structures and provides a declarative way to define validation schemas. Currently at version 2.1.1, released on January 31, 2025, the library maintains an active release cadence with several updates per year, focusing on Python 3 compatibility and modern development practices.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates defining a `Schema` with various `validators`, handling `Invalid` exceptions by calling `to_python`, and using `htmlfill.render` to pre-populate an HTML form with submitted data and error messages.

from formencode import Schema, Invalid
from formencode import validators
from formencode import htmlfill

# 1. Define a Schema
class RegistrationSchema(Schema):
    username = validators.String(not_empty=True, min=3, max=20)
    email = validators.Email(resolve_domain=False)
    age = validators.Int(min=18, max=99)
    password = validators.String(not_empty=True, min=6)
    password_confirm = validators.String(not_empty=True)
    chained_validators = [validators.FieldsMatch('password', 'password_confirm')]

# 2. Example usage: validate form data
form_data_valid = {
    'username': 'testuser',
    'email': 'test@example.com',
    'age': '25',
    'password': 'securepassword',
    'password_confirm': 'securepassword'
}

form_data_invalid = {
    'username': 'tu',
    'email': 'invalid-email',
    'age': '16',
    'password': 'short',
    'password_confirm': 'mismatch'
}

validator = RegistrationSchema()

print('--- Valid Data ---')
try:
    validated_data = validator.to_python(form_data_valid)
    print('Validation successful:', validated_data)
except Invalid as e:
    print('Validation failed:', e.unpack_errors())

print('\n--- Invalid Data ---')
errors_for_html = {}
try:
    validated_data = validator.to_python(form_data_invalid)
    print('Validation successful (should not happen):', validated_data)
except Invalid as e:
    print('Validation failed:')
    errors_for_html = e.unpack_errors()
    for field, msg in errors_for_html.items():
        print(f'  {field}: {msg}')

# 3. Example htmlfill usage (if you have an HTML template)
html_template = '''
<form>
  Username: <input type="text" name="username"><br>
  Email: <input type="text" name="email"><br>
  Age: <input type="text" name="age"><br>
  Password: <input type="password" name="password"><br>
  Confirm Password: <input type="password" name="password_confirm"><br>
  <form:error name="username"/>
  <form:error name="email"/>
  <form:error name="age"/>
  <form:error name="password"/>
  <form:error name="password_confirm"/>
  <form:error name="form"/>
</form>
'''

print('\n--- HTML Fill with Errors ---')
filled_html = htmlfill.render(html_template, form_data_invalid, errors_for_html)
print(filled_html)

view raw JSON →