示例#1
0
class KitchenForm(Form):
    kitchen_foo = Field()

    fisk = Field.multi_choice(
        choices=[1, 2, 3, 4],
        parse=choice_parse,
        initial=[1, 2],
        editable=False
    )

    textarea = Field.textarea(initial='initial value')

    radio = Field.radio(choices=['foo!!_"', 'bar', 'baz'])

    checkbox = Field.boolean()

    date = Field.date()

    choice = Field.choice(choices=['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'X'])

    choice_with_groups = Field.choice(
        choices=['a1', 'a2', 'a3', 'b1', 'b2', 'b3', 'X'],
        choice_to_optgroup=lambda choice, **_:
        choice[0] if choice[0].islower() else None
    )
示例#2
0
    class MyForm(Form):
        name = Field()
        color = Field.choice(choices=['Red', 'Green', 'Blue'])
        in_a_box = html.div(
            children__f1=Field.integer(attrs__class__column=True),
            children__plus=html.span("+",
                                     attrs__class={
                                         'column': True,
                                         'is-narrow': True
                                     }),
            children__f2=Field.integer(attrs__class__column=True, ),
            children__equals=html.span("=",
                                       attrs__class={
                                           'column': True,
                                           'is-narrow': True
                                       }),
            children__f3=Field.integer(attrs__class_column=True),
            iommi_style="bulma_query_form",
            attrs__class={
                'box': True,
                'columns': True,
                'is-vcentered': True
            })

        class Meta:
            iommi_style = "bulma"
            title = "Children that are not fields"
            post_validation = post_valid
            actions__submit__post_handler = on_submit
示例#3
0
    class FruitForm(Form):
        class Meta:
            @staticmethod
            def post_validation(form, **_):
                # Notice that post_validation is run, even if there are invalid fields,
                # so you either have to check that fields that you are interested in
                # are not None, or alternatively if you only want to run your validation if all fields
                # passed their individual checks you can just call form.is_valid (see below).
                # BUT when form.is_valid is called outside of a Form's post_validation
                # handler its result includes errors caused by the post_validation (as you
                # would expect).
                if form.is_valid(
                ) and form.fields.name.value == "tomato" and form.fields.color.value == "Blue":
                    # Or alternatively call form.add_error
                    raise ValidationError("Tomatoes are not blue")

            @staticmethod
            def actions__submit__post_handler(form, **_):
                if form.is_valid():
                    return html.pre(f"You posted: {form.apply(Struct())}"
                                    ).bind(request=request)

        name = Field()
        amount = Field.integer()
        color = Field.choice(choices=['Red', 'Green', 'Blue'], )
示例#4
0
def form_example_children_that_are_not_fields(request):
    def on_submit(form, **_):
        if not form.is_valid():
            return
        return html.pre(f"You posted: {form.apply(Struct())}").bind(request=request)

    def post_validation(form, **_):
        if form.is_valid():
            if form.fields.f1.value + form.fields.f2.value != form.fields.f3.value:
                form.add_error("Calculate again!")

    return Form(
        iommi_style="bulma",
        title="Children that are not fields",
        fields__name=Field(),
        fields__color=Field.choice(choices=['Red', 'Green', 'Blue']),
        fields__in_box=html.div(
            children__f1=Field.integer(attrs__class__column=True),
            children__plus=html.span("+", attrs__class={'column': True, 'is-narrow': True}),
            children__f2=Field.integer(attrs__class__column=True,),
            children__equals=html.span("=", attrs__class={'column': True, 'is-narrow': True}),
            children__f3=Field.integer(attrs__class_column=True),
            iommi_style="bulma_query_form",
            attrs__class={'box': True, 'columns': True, 'is-vcentered': True}
        ),
        post_validation=post_validation,
        actions__submit__post_handler=on_submit
    )
示例#5
0
    class FruitForm(Form):
        class Meta:
            @staticmethod
            def actions__submit__post_handler(form, **_):
                if form.is_valid():
                    return html.pre(f"You posted: {form.apply(Struct())}"
                                    ).bind(request=request)

        name = Field()
        amount = Field.integer()
        color = Field.choice(choices=['Red', 'Green', 'Blue'], )
示例#6
0
    class FruitForm(Form):
        class Meta:
            @staticmethod
            def post_validation(form, **_):
                # Notice that post_validation is run, even if there are invalid fields
                # hence the 'or' below
                if (form.fields.name.value or '').lower(
                ) == "tomato" and form.fields.color.value == "Blue":
                    # Or alternatively call form.add_error
                    raise ValidationError("Tomatoes are not blue")

            @staticmethod
            def actions__submit__post_handler(form, **_):
                if form.is_valid():
                    return html.pre(f"You posted: {form.apply(Struct())}"
                                    ).bind(request=request)

        name = Field()
        amount = Field.integer()
        color = Field.choice(choices=['Red', 'Green', 'Blue'], )
示例#7
0
 class FruitForm(Form):
     name = Field()
     amount = Field.integer()
     color = Field.choice(choices=['Red', 'Green', 'Blue'], )
示例#8
0
 class MyForm(Form):
     my_field = Field.choice(choices=[], )