Back to Examples Run this application locally to try examples

examples/example_multiple_forms.py

from pydal.validators import IS_IN_DB, IS_IN_SET, IS_INT_IN_RANGE, IS_NOT_EMPTY

from py4web import Field, action
from py4web.utils.form import Form, FormStyleDefault

from .common import T, db, flash, session


@action("example_multiple_forms", method=["GET", "POST"])
@action.uses("examples/forms.html", session, db, T)
def example_multiple_forms():
    name = Field("name", requires=IS_NOT_EMPTY())
    forms = [
        Form(
            [Field("name", requires=IS_NOT_EMPTY())],
            form_name="1",
            formstyle=FormStyleDefault,
        ),
        Form(
            [Field("name", requires=IS_NOT_EMPTY())],
            form_name="2",
            keep_values=True,
            formstyle=FormStyleDefault,
        ),
        Form(
            [Field("name", requires=IS_NOT_EMPTY()), Field("age", "integer")],
            form_name="3",
            formstyle=FormStyleDefault,
        ),
        Form(
            [Field("name", requires=IS_NOT_EMPTY()), Field("insane", "boolean")],
            form_name="4",
            formstyle=FormStyleDefault,
        ),
        Form(
            [
                Field("name", requires=IS_NOT_EMPTY()),
                Field("color", requires=IS_IN_SET(["red", "blue", "green"])),
            ],
            form_name="5",
            formstyle=FormStyleDefault,
        ),
        Form(
            [
                Field("name", requires=IS_NOT_EMPTY()),
                Field(
                    "favorite_hero", requires=IS_IN_DB(db, "person.id", "person.name")
                ),
            ],
            form_name="6",
            formstyle=FormStyleDefault,
        ),
    ]
    messages = []
    for form in forms:
        if form.accepted:
            messages.append("form %s accepted with: %s " % (form.form_name, form.vars))
        elif form.errors:
            messages.append("form %s has errors: %s " % (form.form_name, form.errors))
    return dict(forms=forms, messages=messages)

templates/examples/forms.html

[[extend 'layout.html']]

[[for message in messages:]]
<div class="notification is-danger">
[[=message]]
</div>
[[pass]]

[[for form in forms:]]
<h2 class="subtitle">Form [[=form.form_name]]</h2>
<div class="box">
  [[=form]]
</div>
[[pass]]