formulator_howto.txt 9.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
Formulator HOWTO


    This HOWTO is intended to give an introduction to the use of
    Formulator from the Zope Management Interface and from DTML,
    although much of this applies to use from ZPT or Python as
    well. Note that Formulator comes with online help for each tab as
    well as API help, so be sure to check that as well. This document
    will only give an overview of the possibilities and not all the

  Formulator Scope

    Formulator is a tool to create and validate web forms in Zope.
    Formulator takes care of the rendering of the fields in the form,
    as well as the validation and processing of the data that is
    submitted. Formulator's scope is limited to forms: "do web forms,
    web forms only, and web forms well." Formulator does currently not
    even take care of the precise layout of a form -- each form is
    layouted differently and thus layout is left to the developer.
    Formulator does allow for easy integration with external systems,

  Creating a Formulator Form and Fields

    It is easy to create a Formulator Form, just pick it from the add
    list and add it to a folder. I usually only have one form in a
    folder and call it 'form' to make automatic layout handling
    easier; I'll say more about the reason for this later.

    The default view of the form looks just like a folder, except that
    the only things that are addable are Formulator Fields. When you
    add a field to a form, it'll show up in the Form, just like an
    object shows up in a normal Zope Folder.

    When you click on a field, you see a list of its properties in the
    field's 'Edit' screen. This is a good time to explain that
    Formulator has an extensive help system, and that if you click on
    'help' in the 'Edit' screen you'll see a list with a short
    description of what each property does.

    If you click on the 'Test' tab in the Field, you will see the
    field displayed as it would appear in the form. If you fill in
    some value in the field and click on the 'Test' button, you can
    test its validation behavior. If everything could be validated and
    processed all right, you'll see the resulting value. If it could
    not be validated however, you see an error, showing the error_key
    and error_text.

    The best way to learn about what the different fields do and how
    their properties work is to try them out. Just change some
    properties and see what happens in the Test screen. And be sure to
    look at the help.

  Other Form tabs

    The form 'Test' tab is not difficult to explain; it shows all the
    fields you have added to the form. You can test the behavior of
    the entire form here. 

    In the 'Order' part you can group fields and order them inside
    their groups. The order determines the order in which they appear
    on the 'Test' screen, and can also can be used in your own
    code. Initially there is only a single 'Default' group, but you
    can add new groups and change their names.
    In the 'Settings' tab you can determine the form properties.  You
    can set the form submit action and method here, which you can
    later use with the 'header()' and 'footer()' methods of the form.

  Other Field tabs

    The field 'Override' screen allows you to make the field call an
    override method (most commonly a Python Script) for a property.
    Instead of using the property value in the 'Edit' screen, the
    method with the name listed in the override tab will be called to
    retrieve a value then. The returned value must be the same as the
    one that property's field generates; for an IntegerField this is
    an integer, for instance. The titles of overridden fields will be
    displayed between square brackets ('[ ]') in the 'Edit' screen.

    In the 'Messages' screen you can set the text of the error
    messages that field can generate upon validation errors.

  On the examples in this HOWTO

    All the examples in this HOWTO are contained in the file
    'formulator_howto_examples.zexp', which you can download from the
    Formulator product page
    ( and import into
    your Zope. In the examples, all the forms are called 'form'.

  Rendering a form manually with DTML ('manual' folder)

    First, I will show how to use DTML to manually layout a form. This
    takes the most work, but also allows the most flexibility. In all
    these examples I will assume the form is called 'form'.

    The form contains three fields; a StringField 'animal', a
    StringField 'color', and an IntegerField 'number'. 'index_html' is
    the DTML Method that does the manual layout::
      <dtml-var standard_html_header>

      <!-- show the header of the form, using 'Form action' and 
	'Form method' form settings (<form action="..." method="...">)  
      <dtml-var "form.header()">

      <!-- a simple table for layout purposes -->
      <table border="0">

      <!-- each field will be on a line by itself -->

      <!-- first display the title property of the animal field -->
      <td><dtml-var "form.animal.get_value('title')"></td>
      <!-- render the field -->
      <td><dtml-var "form.animal.render()"></td>

      <!-- the same for the color field -->
      <td><dtml-var "form.color.get_value('title')"></td>
      <td><dtml-var "form.color.render()"></td>

      <!-- and the number field -->
      <td><dtml-var "form.number.get_value('title')"></td>
      <td><dtml-var "form.number.render()"></td>

      <!-- the submit button -->
      <td><input type="submit" value=" OK "></td>


      <!-- the form footer -->
      <dtml-var "form.footer()">

      <dtml-var standard_html_footer>

    This shows a form with the three fields. You can easily rearrange
    the layout just by changing the HTML.

  Rendering a form automatically with DTML ('automatic' folder)

    For many simple forms you don't need to do the layout yourself all
    the time. We can use Formulator and acquisition to make layout a
    lot easier. If we know each form is in a separate folder and is
    called 'form', we can place DTML method in the root of the site
    that can render any such form. In this example 'index_html' will
    do the automated rendering directly. In real-world sites you'd
    usually use another method (for instance called 'form_body') to
    render because not all folders would contain forms. In that case
    it'd be easier to put the form rendering code in another method
    (for instance called 'form_body'), which you can then call from
    your other code. Here's 'index_html'::

      <dtml-var standard_html_header>

      <!-- show the header of the form, using 'Form action' and 
	'Form method' form settings (<form action="..." method="...">)  
      <dtml-var "form.header()">

      <!-- a simple table for layout purposes -->
      <table border="0">

      <!-- get a list of all fields in the form -->
      <dtml-in "form.get_fields()">
      <!-- rename each sequence item to 'field' so they can
	   be used more easily -->
      <dtml-let field=sequence-item>

      <!-- each field will be on a line by itself -->    
      <!-- display the title property of this field -->
      <td><dtml-var "field.get_value('title')"></td>
      <!-- render the field -->
      <td><dtml-var "field.render()"></td>


      <!-- the submit button -->
      <td><input type="submit" value=" OK "></td>


      <!-- the form footer -->
      <dtml-var "form.footer()">

      <dtml-var standard_html_footer>

    The nice thing about the automatic approach is that now you can
    change the Formulator form as much as you like; this code will
    always automatically display them. Even better, if you add
    subfolders with forms in them, acquisition makes those forms
    display automatically as well! If you have only simple forms on a
    site, this could be the only DTML Method you need.

  Form validation ('validation' folder)

    I will use the same 'index_html' as in the automatic form
    rendering example and the 'animal/color/number' form to
    demonstrate form validation.

    I've set the 'Form action' property of the form to 'feedback'.
    When the form is submitted it, Zope will access the 'feedback'
    DTML Method. The form data will be coming into 'feedback' in the
    'REQUEST' object (more precisely the 'REQUEST.form' object).

    The 'feedback' method should do a number of things:

      * validate all fields (tell formulator to take care of this).
      * handle any validation errors.

      * if there were no validation errors, do something with the
        form results.

    Here's 'feedback', with comments::

      <dtml-var standard_html_header>
	<!-- try the validation, results should be put in
	     REQUEST (keyed under the field id) --> 
	<dtml-call "form.validate_all_to_request(REQUEST)">  
      <dtml-except FormValidationError>
	<!-- if something went wrong with any field validation,
	     a FormValidationError will be raised, which we
	     will then catch here -->
	<!-- we will display the errors here -->
	<dtml-in "error_value.errors">
	  <dtml-var "field.get_value('title')">:
	  <dtml-var error_text>

	<!-- if no FormValidationError was raised, we're done
	     with validation and our results will now be in
	     REQUEST (and in DTML namespace). -->

	<!-- we could do anything with them, but we'll simply
	     display them -->
	Hah, you are a <dtml-var color> <dtml-var animal> with
	<dtml-var number> legs.


      <dtml-var standard_html_footer> 
    Note that often you can use acquisition with the validation page
    as well, so you can reuse most of its functionality.