Personal tools
You are here: Home Documentation Tutorials Working with Forms in Grok Form Fields extend the information about a Schema Field

Form Fields extend the information about a Schema Field

Understanding the difference between a form field and a schema field.
A walkthrough of the basics of automatically generating HTML forms using Grok, as well as a discussion of a few more advanced Form manipulations.
Page 3 of 9.

Schemas are a collection of schema fields. Schema fields are an extended, abstract description of a Python attribute. From this, we can use schema fields as the basis for automatically generating HTML Forms.

While schema fields provide a rich set of information about attributes, there is even more information that you might want to have when using those schema fields in a form. Thus form objects themselves consist of an ordered collection of form fields. Form fields are distinct from schema fields, and are used to enhance information about a schema field in the context of using that field in a specific Form.

The difference between schema fields and form fields sounds a little confusing, but you can simply auto-generate the form fields from the schema fields.

Typically information in a schema fields provide information about the data Model, independant of how that information might be presented in a user interface. The descriptions for schema fields might only be helpful in the context of a developer or internal business person, but be a poor fit for the user's of the application. However, in many applications you may decide that you don't need this additional seperation of information and wish to directly reuse the information in schema fields to automatically render the user interface and autogenerate the form fields from them.

Form fields contain an attribute named field which is the Schema field object which defines the data for the form field. In addition to this field the other attributes provided by a form field is described in the zope.formlib.interfaces.IFormField Interface.

class IFormField(interface.Interface):
    """Definition of a field to be included in a form

    This should not be confused with a schema field.
    """

    __name__ = schema.ASCII(
        constraint=reConstraint('[a-zA-Z][a-zA-Z0-9_]*',
                                "Must be an identifier"),
        title = u"Field name",
        description=u"""\
        This is the name, without any proefix, used for the field.
        It is usually the same as the name of the for field's schem field.
        """
        )

    field = interface.Attribute(
        """Schema field that defines the data of the form field
        """
        )

    prefix = schema.ASCII(
        constraint=reConstraint('[a-zA-Z][a-zA-Z0-9_]*',
                                "Must be an identifier"),
        title=u"Prefix",
        description=u"""\
        Form-field prefix.  The form-field prefix is used to
        disambiguate fields with the same name (e.g. from different
        schema) within a collection of form fields.
        """,
        default="",
        )

    for_display = schema.Bool(
        title=u"Is the form field for display only?",
        description=u"""\
        If this attribute has a true value, then a display widget will be
        used for the field even if it is writable.
        """
        )

    for_input = schema.Bool(
        title=u"Is the form field for input?",
        description=u"""\
        If this attribute has a true value, then an input widget will be
        used for the field even if it is readonly.
        """
        )

    custom_widget = interface.Attribute(
        """Factory to use for widget construction.

        If not set, normal view lookup will be used.
        """
        )

    render_context = schema.Choice(
        title=u"Should the rendered value come from the form context?",
        description=u"""\

        If this attribute has a true value, and there is no other
        source of rendered data, then use data from the form context
        to set the rendered value for the widget.  This attribute is
        ignored if:

        - There is user input and user input is not being ignored, or

        - Data for the value is passed to setUpWidgets.

        If the value is true, then it is evaluated as a collection of bit
        flags with the flags:

        DISPLAY_UNWRITEABLE
            If the field isn't writable, then use a display widget

            TODO untested


        SKIP_UNAUTHORIZED
            If the user is not priviledges to perfoem the requested
            operation, then omit a widget.

            TODO unimplemented

        """,
        vocabulary=schema.vocabulary.SimpleVocabulary.fromValues((
            False, True,
            DISPLAY_UNWRITEABLE,
            SKIP_UNAUTHORIZED,
            DISPLAY_UNWRITEABLE | SKIP_UNAUTHORIZED,
            )),
        default=False,
        missing_value=False,
        )

    get_rendered = interface.Attribute(
        """Object to call to get a rendered value

        This attribute may be set to a callable object or to
        a form method name to call to get a value to be rendered in a
        widget.

        This attribute is ignored if:

        - There is user input and user input is not being ignored, or

        - Data for the value is passed to setUpWidgets.

        """
        )