Form API reference

class morf.form.Form

The base class for forms

add_field(name, field, after=None, before=None)

Add a field to the form.

Parameters:
  • name – The fieldname to use

  • field – A field object

bind_input(raw, /, validate=True, _internal=False, **kwargs)

Bind the raw submitted value to the field

Field.extract_raw can be used to extract the raw value in the correct format for this method.

bind_object(ob=None, *args, **kwargs)

Bind form values to attributes found in ob

Multiple objects may be specified as additional positional args. In this case properties will be looked up on all objects, with values from later objects overriding earlier listed ones.

Parameters:
  • ob – The object to bind to

  • *args – Additional objects to bind to. Later listed items take precendence over earlier ones

  • **kwargs – Additional keyword arguments to bind to. Takes precedence over all previous arguments.

dimensionality = 'scalar'

Does the field encode a single scalar value, a mapping or a list? Used by RequestBindAdapter to generate appropriate fieldnames

is_considered_empty(value)

Return true if the given value is considered empty, triggering the field’s default value to be used instead.

render_with

alias of FormRenderer

update_object(ob, exclude=frozenset({}))

Update object ob with attributes from the submitted data.

Parameters:
  • ob – the object to update

  • exclude – attributes to skip when updating

morf.form.cleans(*fieldnames, **kwargs)

Register a cleaner function.

Parameters:
  • fieldnames – list of fieldnames to clean. If empty, it will be considered a form-scope validator and only run after all other validation has run and passed.

  • run_always – Applies to form-scope validation only. If True the validation function will be run even when previous validators have failed.

  • before – Run this validator before the given function

morf.form.validates(*fieldnames, **kwargs)

Register a validator function.

Parameters:
  • fieldnames – list of fieldnames to validate. If empty, it will be considered a form-scope validator and only run after all other validation has run and passed.

  • run_always – Applies to form-scope validation only. If True the validation function will be run even when previous validators have failed.

  • before – Run this validator before the given function