class documentation

class Form: (source)

Implements interfaces: klein._form.IForm

View In Hierarchy

A Form is a collection of fields attached to a function.

Class Method rendererFor A form parameter that can render a form declared as a number of fields on another route.
Static Method onValidationFailureFor Register a function to be run in the event of a validation failure for the input to a particular form handler.
Method populateRequestValues Extract the values present in this request and populate a FieldValues object.
Instance Variable fields Form fields
@classmethod
def rendererFor(cls, decoratedFunction, action, method='POST', enctype=RenderableForm.ENCTYPE_FORM_DATA, encoding='utf-8'): (source)

A form parameter that can render a form declared as a number of fields on another route.

Use like so:

    class MyFormApp:
        router = Klein()
        requirer = Requirer()

        @requirer.require(
            router.route("/handle-form", methods=["POST"]),
            name=Field.text(), value=Field.integer(),
        )
        def formRoute(self, name, value):
            ...

        @requirer.require(
            router.route("/show-form", methods=["GET"]),
            form=Form.rendererFor(formRoute)
        )
        def showForm(self, form):
            return form

As a RenderableForm provides IRenderable, you may return the parameter directly

Parameters
decoratedFunction:_requirerFunctionWithFormUndocumented
action:strUndocumented
method:strUndocumented
enctype:strUndocumented
encoding:strUndocumented
Returns
RenderableFormParamUndocumented
@staticmethod
def onValidationFailureFor(handler): (source)

Register a function to be run in the event of a validation failure for the input to a particular form handler.

Generally used like so:

    requirer = Requirer(...)
    @requirer.prerequisite([ISession])
    def procureSession(request):
        return SessionProcurer(...).procureSession(request)
    router = Klein()
    @requirer.require(router.route("/", methods=['POST']),
                      someField=Field.text())
    def formHandler(someField):
        ...
    # Handle input validation failures for handleForm
    @Form.onValidationFailureFor(formHandler)
    def handleValidationFailures(request, fieldValues):
        return "Your inputs didn't validate."
Parameters
handler:_requirerFunctionWithFormThe form handler - i.e. function decorated by Form.handler - for which the decorated function will handle validation failures.
Returns
Callable[[Callable], Callable]a decorator that decorates a function with the signature (request, form) -> thing klein can render.
See Also
defaultValidationFailureHandler for a more detailed description of the decorated function's expected signature. The additional parameter it is passed is a FieldValues instance.
@inlineCallbacks
def populateRequestValues(self, injectionComponents, instance, request): (source)

Extract the values present in this request and populate a FieldValues object.

Parameters
injectionComponents:ComponentizedUndocumented
instance:AnyUndocumented
request:IRequestUndocumented
Returns
Generator[Any, object, None]Undocumented
fields: Sequence[Field] = (source)

Form fields