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)