Form
The Form component is a compatibility wrapper around FastHTML's native Form element. It ensures wildcard imports do not accidentally break ordinary form markup, and it provides FormBuilder.from_pydantic() for generating Bootstrap-styled forms from Pydantic models.
For most use cases, you will use FastHTML's native Form directly, or FormBuilder.from_pydantic() for generated forms.
Bootstrap Reference
Quick Start
Native Form
from fasthtml.common import Form
from faststrap import FormGroup, Input, Button
Form(
FormGroup("Email", Input(name="email", type="email", required=True)),
FormGroup("Password", Input(name="password", type="password", required=True)),
Button("Sign In", type="submit", variant="primary"),
method="post",
action="/login",
)
Generated from Pydantic
from faststrap import FormBuilder
FormBuilder.from_pydantic(
MyPydanticModel,
action="/submit",
method="post",
submit_label="Save",
submit_variant="primary",
)
Visual Examples & Use Cases
1. Basic POST Form
2. Pydantic-Generated Form
3. Form with HTMX
4. File Upload Form
Practical Functionality
1. Form Validation
Faststrap provides several utilities for server-side validation:
from faststrap import FormErrorSummary, FormGroupFromErrors, map_formgroup_validation
# In your route handler:
@app.post("/login")
def login(email: str, password: str):
errors = validate_login(email, password)
if errors:
return FormErrorSummary(errors), FormGroupFromErrors(errors, "email")
return RedirectResponse("/dashboard")
2. Live Validation with HTMX
from faststrap import LiveValidationField
Form(
LiveValidationField(
Input(name="username"),
validate_url="/validate/username",
label="Username",
),
)
3. OTP Input Inside Forms
from faststrap import Form, FormGroup, OTPInput, Button
Form(
FormGroup("Verification Code", OTPInput(length=6, name="otp")),
Button("Verify", type="submit", variant="primary"),
method="post",
action="/verify",
)
Parameter Reference
FormBuilder.from_pydantic
| Parameter | Type | Default | Description |
|---|---|---|---|
model_class |
type[Any] |
required | Pydantic BaseModel class |
action |
str \| None |
None |
Form action URL |
method |
str |
"post" |
HTTP method |
include |
list[str] \| None |
None |
Fields to include |
exclude |
list[str] \| None |
None |
Fields to exclude |
submit_label |
str |
"Submit" |
Submit button text |
submit_variant |
str |
"primary" |
Submit button Bootstrap variant |
form_cls |
str |
"" |
Extra CSS classes for form |
button_cls |
str |
"" |
Extra CSS classes for submit button |
**kwargs |
Any |
{} |
Additional HTML attributes |
Form (Native Wrapper)
The Form wrapper accepts the same arguments as FastHTML's native Form:
| Parameter | Type | Default | Description |
|---|---|---|---|
*children |
Any |
required | Form fields and content |
method |
str |
"post" |
HTTP method |
action |
str \| None |
None |
Form action URL |
enctype |
str |
"application/x-www-form-urlencoded" |
Encoding type |
**kwargs |
Any |
{} |
Additional HTML attributes (hx_, data_, etc.) |
API Reference
faststrap.components.forms.form.FormBuilder
Form builder helpers.
Source code in src/faststrap/components/forms/form.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
from_pydantic(model_class, *, action=None, method='post', include=None, exclude=None, submit_label='Submit', submit_variant='primary', form_cls='', button_cls='', **kwargs)
staticmethod
Generate a Bootstrap-styled form from a Pydantic model class.
Source code in src/faststrap/components/forms/form.py
faststrap.components.forms.form.Form
Compatibility wrapper for FastHTML's native Form element.
Form(...) delegates to FastHTML's native Form so wildcard imports do
not accidentally break ordinary form markup. Form.from_pydantic() is
retained for backward compatibility with Faststrap v0.6.0 and earlier.
New code should prefer FormBuilder.from_pydantic() for generated forms.