Forms
Display and Validation of HTML forms. Powered by pydantic.
Pro-tip: Always validate incoming data.
AirForm
AirForm(initial_data=None)
A form handler that validates incoming form data against a Pydantic model. Can be used with awaited form data or with FastAPI's dependency injection system.
Example:
class FlightModel(BaseModel):
flight_number: str
destination: str
class FlightForm(air.AirForm):
model = FlightModel
@app.post("/flight")
async def flight_form(request: air.Request):
"Awaited form data"
flight = await FlightForm.from_request(request)
if flight.is_valid:
return air.Html(air.H1(flight.data.flight_number))
return air.Html(air.H1(air.Raw(str(len(flight.errors)))))
@app.post("/flight-depends")
async def flight_form_depends(flight: Annotated[FlightForm, Depends(FlightForm())]):
"Dependency injection"
if flight.is_valid:
return air.Html(air.H1(flight.data.flight_number))
return air.Html(air.H1(air.Raw(str(len(flight.errors)))))
NOTE: This is named AirForm to avoid collisions with tags.Form
Source code in src/air/forms.py
57 58 59 60 61 |
|
widget
property
widget
Widget for rendering of form in HTML
If you want a custom widget, replace with a function that accepts:
- model: BaseModel
- data: dict|None
- errors:dict|None=None
AirField
AirField(
default=None,
*,
type=None,
label=None,
default_factory=None,
alias=None,
autofocus=False,
title=None,
description=None,
gt=None,
ge=None,
lt=None,
le=None,
multiple_of=None,
min_length=None,
max_length=None,
pattern=None,
max_digits=None,
decimal_places=None,
examples=None,
deprecated=None,
exclude=False,
discriminator=None,
frozen=None,
validate_default=None,
repr=True,
init_var=None,
kw_only=None,
json_schema_extra=None,
**extra,
)
A wrapper around pydantic.Field to provide a cleaner interface for defining special input types and labels in air forms.
NOTE: This is named AirField to adhere to the same naming convention as AirForm.
Example:
class CheeseModel(BaseModel):
name: str = air.AirField(label="Name", autofocus=True)
age: int
class CheeseForm(air.AirForm):
model = CheeseModel
Source code in src/air/forms.py
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 |
|
errors_to_dict
errors_to_dict(errors)
Converts a pydantic error list to a dictionary for easier reference.
Source code in src/air/forms.py
157 158 159 160 161 |
|
get_user_error_message
get_user_error_message(error)
Convert technical pydantic error to user-friendly message.
Source code in src/air/forms.py
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 |
|
pydantic_type_to_html_type
pydantic_type_to_html_type(field_info)
Return HTML type from pydantic type.
Default to 'text' for unknown types.
Source code in src/air/forms.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|