1.5.2.4 Form

The is the main CherryClass of the module. To create a form, you should declare a CherryClass that inherits from Form.

You may use the following variables and methods:

variable: method
String containing the method attribute of the form tag. It may be send or post. The default value is post
variable: enctype
String containing the enctype attribute of the form tag. For instance, for a form that allows the user to upload files, you would use multipart/form-data The default value is an empty string, which means that the enctype attribute wile be omitted.
variable: fieldList
List containing instances of the FormField and FormInstance CherryClasses. This list determines which fields and separators will be displayed, and in which order. fieldList should be set in the __init__ method of the CherryClass.

function: formView(leaveValues=0)
This function returns the HTML code for the form. if leaveValues is false, it will use the default value for each of the fields. If leaveValues is true, it will use the values that are in request.paramMap (in other words, the values that were entered by the user)

function: validateFields()
This function should be overwritten if you need to perform some validation that involves several fields at the same time (for instance, checking that 2 passwords match).

If a field has an error, the function should set the errorMessage member variable of the FormField instance.

function: setFieldErrorMessage(fieldName, errorMessage)
Sets the errorMessage member variable of the FormField instance whose name is fieldName.

function: getFieldOptionList(fieldName)
Returns the optionList member variable of the FormField instance whose name is fieldName.

function: getFieldDefaultValue(fieldName)
Returns the defaultValue member variable of the FormField instance whose name is fieldName.

function: setFieldDefaultValue(fieldName, defaultValue)
Sets the defaultValue member variable of the FormField instance whose name is fieldName.

function: getFieldNameList(exceptList=[])
Returns the list of field names, based on the fieldList member variable. Names that are in exceptList are omitted.

function: validateForm()
This function checks if the data that the user entered is correct or not. It returns 1 if it is, 0 otherwise.

view: postForm(**kw)
This view is automatically called when the user submits the form. You should overwrite this view and add your own code to handle the form data. Typical code for this view looks like this:
def postForm(self, **kw):
    if self.validateForm():
        # Yes, the data is correct
        # Do what you want here
        pass
    else:
        # No, the data is incorrect
        # Redisplay the form and tell the user to fix the errors:
        return "<html><body><font color=red>Fill out missing fields</font>"+self.formView(1)+"</body></html>"

See About this document... for information on suggesting changes.