Skip to content

Forms

AirForm

AirForm(initial_data=None)

A form handler that validates incoming form data against a Pydantic model. To be used with FastAPI's dependency injection system.

class CheeseModel(pydantic.BaseModel):
    name: str
    age: int

class CheeseForm(air.AirForm):
    model = CheeseModel

@app.post("/cheese")
async def cheese_form(cheese: Annotated[CheeseForm, Depends(CheeseForm())]):
    if cheese.is_valid:
        return air.Html(air.H1(cheese.data.name))
    return air.Html(air.H1(air.Raw(str(len(cheese.errors)))))

NOTE: This is named AirForm to avoid collisions with tags.Form

Source code in src/air/forms.py
44
45
46
47
def __init__(self, initial_data: dict | None = None):
    if self.model is None:
        raise NotImplementedError("model")
    self.initial_data = initial_data

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, 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.

Source code in src/air/forms.py
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
181
182
183
184
185
186
187
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
def AirField(
    default: Any = None,
    *,
    type: str | None = None,
    label: str | None = None,
    default_factory: Callable[[], Any] | None = None,
    alias: str | None = None,
    title: str | None = None,
    description: str | None = None,
    gt: float | None = None,
    ge: float | None = None,
    lt: float | None = None,
    le: float | None = None,
    multiple_of: float | None = None,
    min_length: int | None = None,
    max_length: int | None = None,
    pattern: str | None = None,
    max_digits: int | None = None,
    decimal_places: int | None = None,
    examples: list[Any] | None = None,
    deprecated: bool | str | None = None,
    exclude: bool = False,
    discriminator: str | None = None,
    frozen: bool | None = None,
    validate_default: bool | None = None,
    repr: bool = True,
    init_var: bool | None = None,
    kw_only: bool | None = None,
    json_schema_extra: dict | None = None,
    **extra: Any,
) -> Any:
    """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.
    """
    if json_schema_extra is None:
        json_schema_extra = {}
    if type:
        json_schema_extra[type] = True
    if label:
        json_schema_extra["label"] = label

    return Field(
        default,
        json_schema_extra=json_schema_extra,
        default_factory=default_factory,
        alias=alias,
        title=title,
        description=description,
        gt=gt,
        ge=ge,
        lt=lt,
        le=le,
        multiple_of=multiple_of,
        min_length=min_length,
        max_length=max_length,
        pattern=pattern,
        max_digits=max_digits,
        decimal_places=decimal_places,
        examples=examples,
        deprecated=deprecated,
        exclude=exclude,
        discriminator=discriminator,
        frozen=frozen,
        validate_default=validate_default,
        repr=repr,
        init_var=init_var,
        kw_only=kw_only,
        **extra,
    )

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
112
113
114
115
116
def errors_to_dict(errors: list[dict] | None) -> dict[str, dict]:
    "Converts a pydantic error list to a dictionary for easier reference."
    if errors is None:
        return {}
    return {error["loc"][0]: error for error in errors}

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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
def pydantic_type_to_html_type(field_info: Any) -> str:
    """Return HTML type from pydantic type.

    Default to 'text' for unknown types.
    """
    special_fields = [
        "hidden",
        "email",
        "password",
        "url",
        "datedatetime-local",
        "month",
        "time",
        "color",
        "file",
    ]
    for field in special_fields:
        if field_info.json_schema_extra and field_info.json_schema_extra.get(
            field, False
        ):
            return field

    return {int: "number", float: "number", bool: "checkbox", str: "text"}.get(
        field_info.annotation, "text"
    )