physics_workload.app.forms.academic_group

Classes

AcademicGroupDetailForm

Describe a Form. Example:

Module Contents

class physics_workload.app.forms.academic_group.AcademicGroupDetailForm(**kwargs)

Bases: iommi.Form

Describe a Form. Example:

class MyForm(Form):
    a = Field()
    b = Field.email()

form = MyForm().bind(request=request)

You can also create an instance of a form with this syntax if it’s more convenient:

form = Form(
    fields=dict(
        a=Field(),
        b=Field.email(),
    ),
).bind(request=request)

In the common case the fields namespace will contain only instances of Field, but iommi actually supports arbitrary Part objects. For example:

form = Form(
    fields=dict(
        # Display a and b inside a box
        box=html.div(
            attrs__class__box=True,
            children__a=Field(),
            children__b=Field.email(),
        ),
        # And c regularly
        c=Field(),
    )
)

So that writing the application logic (e.g. validation and post handlers) is independent of minor changes to the layout, after bind the fields namespace of the form will contain only instances of Field keyed by their _name independently of how deep they are in the hierarchy. Given the above, an appropriate post_handler would be:

def post_handler(form, **_):
    if not form.is_valid():
        return

    print(form.fields.a.value, form.fields.b.value, form.fields.c.value)
    # And not:
    # print(form.fields.box.a.value, form.fields.box.b.value, form.fields.c.value)

# @test
assert post_handler(form=Struct(is_valid=lambda: False)) is None

post_handler(form.bind(request=req('post')))
# @end
class Meta
auto
fields
editable = False
include
instance
iommi_style