API Reference¶
AirForm: Display and Validation of HTML forms. Powered by pydantic.
Pro-tip: Always validate incoming data.
AirForm
¶
A form handler that validates and renders Pydantic models as HTML.
Daniel's original pattern: the form knows its model, validates from a dict, and renders itself through a swappable widget.
CSRF protection is automatic. render() embeds a signed token as a hidden input. validate() pops and checks the token before Pydantic sees the data. If validate() is called directly without render() (programmatic use, tests), CSRF is skipped.
Example::
from pydantic import BaseModel
from airform import AirForm
class WatercolorModel(BaseModel):
pigment: str
opacity: int
class WatercolorForm(AirForm[WatercolorModel]):
pass
form = WatercolorForm()
form.validate({"pigment": "burnt sienna", "opacity": 80})
if form.is_valid:
print(form.data.pigment)
# Render a blank form (includes CSRF token automatically)
html = WatercolorForm().render()
# Swap the widget for custom rendering
class CustomWatercolorForm(AirForm[WatercolorModel]):
widget = my_custom_renderer
Source code in src/airform/forms.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
data
property
¶
The validated model instance.
Raises:
| Type | Description |
|---|---|
AttributeError
|
If accessed before successful validation. |
from_request(request)
async
classmethod
¶
Create and validate an AirForm instance from a request.
CSRF is always enforced for browser submissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
An object with an async |
required |
Returns:
| Type | Description |
|---|---|
Self
|
An AirForm instance with validation results. |
Source code in src/airform/forms.py
render()
¶
Render the form as HTML using the widget.
Automatically embeds a signed CSRF token as a hidden field. Uses submitted data if available (preserves values after validation errors), falls back to initial_data.
Source code in src/airform/forms.py
save_data()
¶
Return validated data as a dict, excluding save-excluded fields.
Use this when persisting to a database::
await MyModel.create(**form.save_data())
Source code in src/airform/forms.py
validate(form_data)
¶
Validate form data against the model.
If render() or from_request() set a CSRF expectation, the token is popped and checked before Pydantic sees the data. Fields in _save_excludes are excluded from form.data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
form_data
|
Mapping[str, Any]
|
Mapping containing the form fields to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if validation succeeds, False otherwise. |
Source code in src/airform/forms.py
SafeHTML
¶
Bases: str
A str subclass that HTML-aware systems can trust without escaping.
Follows the html protocol used by Jinja2 and MarkupSafe. Air's tag system checks for html and skips escaping, so form.render() output can be passed directly to Air tags without air.Raw() wrapping.
Source code in src/airform/forms.py
__html__()
¶
Return self for the html protocol.
Jinja2/MarkupSafe calls obj.html() and expects an HTML string back. Air's tag system checks hasattr(child, 'html') and skips escaping. This satisfies both.
Source code in src/airform/forms.py
default_css()
cached
¶
default_form_widget(*, model, data=None, errors=None, excludes=None)
¶
Render form fields for a Pydantic model as HTML.
Reads the full AirField metadata vocabulary: Widget, Label, Placeholder, HelpText, Choices, Autofocus, PrimaryKey, Hidden, ReadOnly.
This is the default widget for AirForm.render(). Swap it by
setting widget on your AirForm subclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
type[BaseModel]
|
The Pydantic model class to render. |
required |
data
|
dict | None
|
Dictionary of data to pre-populate fields. |
None
|
errors
|
list | None
|
List of Pydantic validation errors. |
None
|
excludes
|
set[str] | None
|
Field names to skip when rendering. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
HTML string with all form fields. |
Source code in src/airform/forms.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
errors_to_dict(errors)
¶
Converts a pydantic error list to a dictionary for easier reference.
get_user_error_message(error)
¶
Convert technical pydantic error to user-friendly message.
Source code in src/airform/forms.py
label_for_field(field_name, field_info)
¶
Return the label for a field from AirField metadata.
pydantic_type_to_html_type(field_info)
¶
Return HTML input type from a Pydantic field's type and metadata.
Checks AirField metadata first (Widget, Choices), then infers from the Python type annotation.