z3c.form表单管理

z3c.form表单管理
[align=center]z3c.form表单管理
[/align]本章样例代码将用到的接口及内容类:
  >>> import zope.interface
  >>> import zope.schema
  >>> class IPerson(zope.interface.Interface):
  ...
  ...     id = zope.schema.TextLine(
  ...         title=u'ID',
  ...         readonly=True,
  ...         required=True)
  ...
  ...     name = zope.schema.TextLine(
  ...         title=u'Name',
  ...         required=True)
  ...
  ...     gender = zope.schema.Choice(
  ...         title=u'Gender',
  ...         values=('male', 'female'),
  ...         required=False)
  ...
  ...     age = zope.schema.Int(
  ...         title=u'Age',
  ...         description=u"The person's age.",
  ...         min=0,
  ...         default=20,
  ...         required=False)
  ...
  ...     @zope.interface.invariant
  ...     def ensureIdAndNameNotEqual(person):
  ...         if person.id == person.name:
  ...             raise zope.interface.Invalid(
  ...                 "The id and name cannot be the same.")

  >>> from zope.schema.fieldproperty import FieldProperty
  >>> @zope.interface.implementer(IPerson)
  ... class Person(object):
  ...     id = FieldProperty(IPerson['id'])
  ...     name = FieldProperty(IPerson['name'])
  ...     gender = FieldProperty(IPerson['gender'])
  ...     age = FieldProperty(IPerson['age'])
  ...
  ...     def __init__(self, id, name, gender=None, age=None):
  ...         self.id = id
  ...         self.name = name
  ...         if gender:
  ...             self.gender = gender
  ...         if age:
  ...             self.age = age
  ...
  ...     def __repr__(self):
  ...         return '<%s %r>' % (self.__class__.__name__, self.name)
Okay, that should suffice for now.

What's next? Well, first things first. Let's create an add form for the
person. Since practice showed that the ``IAdding`` interface is overkill for
most projects, the default add form of ``z3c.form`` requires you to define the
creation and adding mechanism.
**Note**:

[align=left] If it is not done, ``NotImplementedError``
[/align]
设置