Using Class and New in CForms

- TARGET-AUDIENCE: *beginner*

What you will get from this page

An understanding of the more complex types of form that can be built with Cocoon.

Your basic skills

An understanding of building simple form model, template.

Technical prerequisites

Ideally a working forms+flow sitemap in which to experiment.

page metadata

- AUTHOR: AndrewSavory

It is possible to build complex forms using the <fd:class/> widget.

<fd:class/> is a meta-widget, which can be constructed from one or more existing widgets.

<fd:new/> is an instance of the meta-widget, where you are actually using it.

Consider the following basic form:

<?xml version="1.0" encoding="UTF-8"?>
<fd:form xmlns:fd=""

        <!-- A basic text widget -->
        <fd:field id="basic-text" required="true">
            <fd:datatype base="string"/>
            <fd:label>Basic Text Widget</fd:label>

        <!-- Basic cancel/submit -->
        <fd:submit id="ok" action-command="ok" validate="true">
        <fd:submit id="cancel" action-command="cancel" validate="false">


... and an accompanying template:

<?xml version="1.0" encoding="UTF-8"?>


        <ft:form-template action="" method="POST">

            <!-- Use the basic widget -->
            <p><ft:widget-label id="basic-text"/>: <ft:widget id="basic-text"/></p>

            <p class="form-buttons">
                <ft:widget id="cancel"/>
                <ft:widget id="ok"/>


To create a very basic meta-widget, we need to add a definition to the model:

<!-- A class widget -->
<!-- This defines a meta-widget "basic-class-text" -->
<fd:class id="basic-class-text">
        <fd:field id="class-text" required="true">
            <fd:datatype base="string"/>
            <fd:label>Class Text Widget</fd:label>

In order to use this definition, we also need to instantiate it within the model. We do this as follows:

<!-- Instantiate the widget -->
<fd:new id="basic-class-text"/>

In our template, we first need to define how the meta-widget should be displayed:

<!-- Define the *template* for our meta-widget -->
    <ft:class id="basic-class-text">
        <ft:widget-label id="class-text"/> : <ft:widget id="class-text"/>

... and then we use it in the template as follows:

<!-- Use the meta-widget -->
<ft:new id="basic-class-text"/>

More complex meta-widgets can be built up. For example, a meta-widget with two text fields:

<!-- A class widget -->
<!-- This defines a more complicated meta-widget "complex-class-text" -->
<fd:class id="complex-class-text">
        <fd:field id="complex-class-text-one" required="true">
            <fd:datatype base="string"/>
            <fd:label>Complex Class Text Widget One</fd:label>
        <fd:field id="complex-class-text-two" required="true">
            <fd:datatype base="string"/>
            <fd:label>Complex Class Text Widget Two</fd:label>

<!-- Instantiate the widget -->
<fd:new id="complex-class-text"/>

... and is used in the template as follows:

<!-- Define the *template* for our complex meta-widget -->
    <ft:class id="complex-class-text">
        <ft:widget-label id="complex-class-text-two"/> : <ft:widget id="complex-class-text-two"/><br />
        <ft:widget-label id="complex-class-text-one"/> : <ft:widget id="complex-class-text-one"/>

<!-- Use the complex meta-widget -->
<ft:new id="complex-class-text"/>

The output of this looks something like the following screenshot:
To bind this form to an XML file is simple. Given an XML file as follows:

<?xml version="1.0" encoding="UTF-8"?>
    <basic-text>test basic</basic-text>
    <class-text>test class</class-text>
    <complex-class-text-one>complex one</complex-class-text-one>
    <complex-class-text-two>complex two</complex-class-text-two>

The binding file would look like this:

<?xml version="1.0" encoding="UTF-8"?>

    <fb:value id="basic-text" path="basic-text"/>

    <fb:class id="basic-class-text">
        <fb:value id="class-text" path="class-text"/>

    <fb:new id="basic-class-text"/>
    <fb:class id="complex-class-text">
        <fb:value id="complex-class-text-one" path="complex-class-text-one"/>
        <fb:value id="complex-class-text-two" path="complex-class-text-two"/>
    <fb:new id="complex-class-text"/>


FormsClassWidget (last edited 2009-09-20 23:41:27 by localhost)