WTForms Components
WTForms-Components is a library that extends WTForms with additional fields, validators, and widgets, aiming to simplify common form-related tasks. It is currently at version 0.11.0 and is maintained with occasional releases driven by community contributions and WTForms compatibility.
Common errors
-
ModuleNotFoundError: No module named 'wtforms-components'
cause The user attempted to import the package using its PyPI name (`wtforms-components`) instead of its Python module name (`wtforms_components`).fixChange your import statement from `import wtforms-components` to `import wtforms_components` (or `from wtforms_components import ...`). -
ImportError: cannot import name 'Unique' from 'wtforms_components.validators' (or similar for 'Email', 'DateField')
cause Many commonly used fields and validators from `wtforms-components` are directly exposed under the top-level `wtforms_components` namespace, not nested within submodules like `wtforms_components.validators` or `wtforms_components.fields`.fixImport the symbol directly from the top-level package, e.g., `from wtforms_components import Unique`. -
TypeError: __init__() missing 1 required positional argument: 'model' (or 'query' for Unique validator)
cause The `Unique` validator (and similar database-interacting validators) requires specific arguments like a database model and field to check uniqueness against, which were not provided or were provided incorrectly.fixWhen using `Unique`, ensure you pass the SQLAlchemy/ORM model and the field to check uniqueness for. Example: `email = Email(validators=[Unique(User, User.email)])`.
Warnings
- gotcha The PyPI package name is `wtforms-components` (with a hyphen), but the Python package name for importing is `wtforms_components` (with an underscore). Importing `wtforms-components` will result in a `ModuleNotFoundError`.
- gotcha While the library declares `WTForms>=2.0`, specific functionalities might be primarily tested against older WTForms 2.x versions. Users on WTForms 3.x or 4.x should thoroughly test custom fields, widgets, and complex validators for full compatibility, especially if encountering unexpected `TypeError` or `AttributeError`.
Install
-
pip install wtforms-components
Imports
- Email
from wtforms_components.validators import Email
from wtforms_components import Email
- DateField
from wtforms_components.fields import DateField
from wtforms_components import DateField
- Unique
from wtforms_components.validators import Unique
from wtforms_components import Unique
Quickstart
from wtforms import Form
from wtforms_components import Email, DateField
from wtforms.validators import DataRequired
class UserProfileForm(Form):
email = Email(validators=[DataRequired()])
birth_date = DateField(validators=[DataRequired()])
# Example usage (run with a dictionary or request.form)
form_data = {
'email': 'user@example.com',
'birth_date': '1990-05-15'
}
form = UserProfileForm(data=form_data)
if form.validate():
print("Form is valid!")
print(f"Email: {form.email.data}")
print(f"Birth Date: {form.birth_date.data}")
else:
print("Form errors:")
for field, errors in form.errors.items():
for error in errors:
print(f" {field}: {error}")