input.SimpleForm

  1. Introduction
  2. FormHandler() Class
  3. FormHandler Methods
  4. FormHandler Validator and Conversion Methods
  5. Example

Introduction

FormHandler() Class

The FormHandler() class acts like a dictionary, but with extra functionality. This class is used by instantiating it, then optionally calling validators on it's fields, and finally using the data in the field.

Unlike the standard Python form handler, the values associated with the keys in the dictionary are actual values, and do not need to have their "value" method called to get to the submitted data.

from jotweb.input import SimpleForm

form = SimpleForm.FormHandler()
#  validate fields
if form.vdt_required('EmailAddr'):
   form.vdt_emailaddr('EmailAddr')
if form.vdt_required('FullName'):
   form.vdt_realname('FullName'
   form.vdt_length('FullName', 3, 20)

print 'Subscribing user "%(FullName)s with address %(EmailAddr)s' % form

Note that if the above is placed in a "formhandler.py" file, elements of the form can be accessed from TAL/HTML with a path like "formhandler/form/EmailAddr", and error checking from the HTML can be done with the path "formhandler/form/geterrors".

SimpleForm.FormHandler(dataIn = None)

Arguments:

Description: This call instantiates a FormHandler object, using either the form filled data passed by the client, or using the dictionary passed in via the "dataIn" argument.

Note that if dataIn is not passed, this call will consume the form data submitted by the user. Multiple calls without dataIn set will produce the same results, because a local copy of the consumed form is kept. Calling something else which consumes the form will not work.

FormHandler Methods

FormHandler Validator and Conversion Methods

These methods are used for validating form fields. They take an optional "msg" argument which can be used to override the default message relating to form validation failure. These strings may include several "%(keys)s" strings, which will be replaced by appropriate values in the form validation step. As noted in the specific validator description, some validators augment this information. All validators provide the following keys:

The conversion methods are:

  • cvt_makelowercase(field, msg = None)

    Returns: Always succeeds.

    Side-effects: Converts field to lowercase.

    Description: Converts the letters in the field to lower-case.

  • cvt_makeuppercase(field, msg = None)

    Returns: Always succeeds.

    Side-effects: Converts field to uppercase.

    Description: Converts the letters in the field to upper-case.

  • The validator methods are:

    Example

    Simple Form

    Create a file called "simpleform.html", which contains the form HTML:

       <form action="simplepost.html" method="POST">
          Email Address: <input type="text" name="EmailAddr" /><br />
          Full Name: <input type="text" name="FullName" /><br />
          <input type="submit" name="subscribe" value="Subscribe" /><br />
       </form>

    Create a file called "simplepost.html", which contains the HTML which validates and displays the results of the form:

       <div tal:define="form formchk/form">
          <div tal:condition="not:form/geterrors">
             <!-- Form is valid -->
                <h1><font color="#00ff00">Form Success</font></h1>
                Full Name: <span tal:replace="form/FullName">Full Name</span><br />
                Email Addr: <span tal:replace="form/EmailAddr">Full Name</span><br />
          </div>
    
          <div tal:condition="form/geterrors">
             <!-- Form had validation errors -->
             <h1><font color="#ff0000">Form Errors</font></h1>
             <p />The following errors were found in your form input:
             <ul>
                <li tal:repeat="value form/geterrors" tal:content="value">Error</li>
             </ul>
          </div>
       </div>

    Finally, set up the form validator as the file "formchk.py":

       from jotweb.input import SimpleForm
    
       form = SimpleForm.FormHandler()
    
       if form.vdt_required('EmailAddr'):
          form.vdt_emailaddr('EmailAddr')
       if form.vdt_required('FullName'):
          form.vdt_realname('FullName')
          form.vdt_length('FullName', 3, 20)

    See Examples

    JOTWeb includes a number of examples, one of which is a slightly complicated form handling routine using SimpleForm(). Please see the "examples" directory in your JOTWeb installation, in particular the "form.html" and "formpy.py" files. This example is similar to the above, but slightly more complicated.

    Index