plone.autoform builds custom z3c.form forms based on a model (schema) of what fields to include and what widgets and options should be used for each field. This model is defined as a zope.schema-based schema, but additional hints can be supplied to control aspects of form display not normally specified in a Zope schema.
Basic schema-based forms
To use the automatic form setup, mix in the following base class in your forms:
>>> from plone.autoform.form import AutoExtensibleForm
and then provide the schema (a schema interface) and optionally the additionalSchemata (a list of schema interfaces) attributes on your form:
class MyForm(AutoExtensibleForm, form.EditForm):
schema = IMySchema
additionalSchemata = (ISchemaOne, ISchemaTwo,)
For dynamic forms, you could of course make schema and additionalSchemata into properties. For example, plone.dexterity extends the basic AutoExtensibleForm so that schema is the content type schema and additionalSchemata is a list of field provider schemas associated with behaviors.
Controlling form presentation
Directives can be specified in the schema to control aspects of form presentation.
Changing a field's display mode
A field's widget can be displayed in several "modes":
input - allows the user to enter data into the field
display - a read-only indication of the field's value
hidden - a record of the field's value that is included only in the HTML source
The mode can be controlled using the mode directive:
from plone.supermodel import model
from plone.autoform import directives as form
class IMySchema(model.Schema):
form.mode(secret=hidden )
form.mode(IEditForm, secret=input )
secret = schema.TextLine(
title=u"Secret",
default=u"Secret stuff (except on edit forms)"
)
In this case the mode for the secret field is set to hidden for most forms, but input for forms that provide the IEditForm interface.
The corresponding supermodel XML directive is form:mode:
Secret
Secret stuff (except on edit forms)
The mode can be specified briefly if it should be the same for all forms:
Secret
Secret stuff
In other words, form:mode may be either a single mode, or a space-separated list of form_interface:mode pairs.
Omitting fields
A field can be omitted entirely from all forms, or from some forms, using the omitted and no_omit diretives. In this example, the dummy field is omitted from all forms, and the edit_only field is omitted from all forms except those that provide the IEditForm interface:
from z3c.form.interfaces import IEditForm
from plone.supermodel import model
from plone.autoform import directives as form
class IMySchema(model.Schema):
form.omitted(dummy )
dummy = schema.Text(
title=u"Dummy"
)
form.omitted(edit_only )
form.no_omit(IEditForm, edit_only )
edit_only = schema.TextLine(
title = u'Only included on edit forms',
)
In supermodel XML, this can be specified as:
Dummy
Only included on edit form
form:omitted may be either a single boolean value, or a space-separated list of form_interface:boolean pairs.
Re-ordering fields
A field's position in the form can be influenced using the order_before and order_after directives. In this example, the not_last field is placed before the summary field even though it is defined afterward:
from plone.supermodel import model
from plone.autoform import directives as form
class IMySchema(model.Schema):
summary = schema.Text(
title=u"Summary",
description=u"Summary of the body",
readonly=True
)
form.order_before(not_last=summary )
not_last = schema.TextLine(
title=u"Not last",
)
The value passed to the directive may be either '* (indicating before or after all fields) or the name of another field. Use .fieldname' to refer to field in the current schema or a base schema. Prefix with the schema name (e.g. IDublinCore.title ) to refer to a field in another schema. Use an unprefixed name to refer to a field in the current or the default schema for the form.
In supermodel XML, the directives are called form:before and form:after. For example:
Not last
Organizing fields into fieldsets
Fields can be grouped into fieldsets, which will be rendered within an HTML |