Package aquarium :: Package widget :: Module FormUtil :: Class FormUtil
[show private | hide private]
[frames | no frames]

Class FormUtil

AquariumClass --+
                |
   HasFriends --+
                |
               FormUtil


This is a utility to build consistent HTML forms.

The purpose of this class is not to "abstract" HTML. Its purpose is to provide "macros" so that you can get things done more quickly and consistently. If your needs are very generic, you can use high-level macros like field. In your needs are less generic, you can use middle-level macros like textField and label. At the bottom are very low-level macros like getErrorMsg and getDefault. Naturally, you can subclass this class as needed.

This class also mixes in the HasFriends mixin. That means if you need to add a new method fooField, simply create a new module called aquarium.widget.formutil.fooField containing a function called fooField. The fooField function should accept a FormUtil instance called self as its first argument (i.e. it acts like a method in this class). Then fooField will just work.

The following attributes are used:

defaults
This is a list of dicts. When looking for a default value, the list will be traversed, and the first dict to define the given name will be used for the value.
errors
This is a dict where each key is a field name and each value is the error message associated with that field. Fields that do not have an error will not be present in this dict. If left to its default value in the constructor, I'll look in actionResults.errors. I'll look for actionResults in both ctx and session.

The following class attributes are used:

labelClass
This is the CSS class for labels.
errorClass
This is the CSS class for errors.

Method Summary
  __init__(self, ctx, defaults, errors)
Accept the arguments.
  attributes(self, attributes)
Return a set of HTML tag attributes.
  checkBoxField(self, name, value, **attributes)
Returns a string representing a checkbox.
  field(self, label, name, type, **attributes)
Build a field with a label in a table row.
  fileField(self, name, **attributes)
Returns a string representing a file upload field.
  focusField(self, form, field)
Output the JavaScript to focus a field.
  getDefault(self, name)
Return the default value for the given field.
  getErrorMsg(self, name, brk, **attributes)
Return the error message for a field, if present.
  ifError(self, name, if_, else_)
Return if_ if there was an error, else_ otherwise.
  inputField(self, name, type, **attributes)
Returns a string representing an input field of type type.
  label(self, name, label, brk, **attributes)
Return a label and the error message defined by the validator.
  optionField(self, option, selectedValue)
Return an option for use within a select.
  passwordField(self, name, providePasswordDefault, **attributes)
Returns a string representing a password field.
  radioField(self, name, value, **attributes)
Returns a string representing a radio button.
  selectField(self, name, options, **attributes)
Returns a string representing a fully fledged select box.
  textAreaField(self, name, **attributes)
Returns a string representing a textarea.
  textField(self, name, **attributes)
Returns a string representing a text field.
    Inherited from HasFriends
  __getattr__(self, attr)
Return the desired friend method.

Class Variable Summary
str errorClass = 'error'
str labelClass = 'label'

Method Details

__init__(self, ctx, defaults=None, errors=None)
(Constructor)

Accept the arguments.

Overrides:
aquarium.util.AquariumClass.AquariumClass.__init__

attributes(self, attributes)

Return a set of HTML tag attributes.

checkBoxField(self, name, value, **attributes)

Returns a string representing a checkbox.

value
This will be used for the value attribute of the field. It should not be confused with the value coming from self.defaults.

field(self, label, name, type, **attributes)

Build a field with a label in a table row.

type
This can be any "blah" for which there exists a "blahField".

fileField(self, name, **attributes)

Returns a string representing a file upload field.

focusField(self, form, field)

Output the JavaScript to focus a field.

Use this to set the default focus of a form. Call this after you have output the given field.

form
This is the name attribute of the <form> tag.
field
This is the name or id attribute of the field.

getDefault(self, name)

Return the default value for the given field.

I will not run htmlent on it. If no default can be found, I'll return "".

getErrorMsg(self, name, brk=True, **attributes)

Return the error message for a field, if present.

name
The name of the field we're dealing with.
brk
If True, use a div, otherwise use a span.
attributes
These will be used for the div or span.

I will wrap it with the CSS class self.errorClass.

ifError(self, name, if_, else_='')

Return if_ if there was an error, else_ otherwise.

inputField(self, name, type, **attributes)

Returns a string representing an input field of type type.

label(self, name=None, label=None, brk=True, **attributes)

Return a label and the error message defined by the validator.

Wrap it with the CSS class self.labelClass.

name
If None do not attempt to display an error message.
label
If None do not show a label but only show the error message.
brk
If True, use a div, otherwise use a span.
attributes
These will be used for the div or span of the label, but they won't be passed onto self.getErrorMsg.

optionField(self, option, selectedValue)

Return an option for use within a select.

option
This is either a single value to be used as both the label and the value or a tuple of the form (label, value).

passwordField(self, name, providePasswordDefault=False, **attributes)

Returns a string representing a password field.

radioField(self, name, value, **attributes)

Returns a string representing a radio button.

value
This will be used for the value attribute of the field. It should not be confused with the value coming from self.defaults.

selectField(self, name, options, **attributes)

Returns a string representing a fully fledged select box.

options
See the option argument of optionField.

textAreaField(self, name, **attributes)

Returns a string representing a textarea.

textField(self, name, **attributes)

Returns a string representing a text field.


Class Variable Details

errorClass

Type:
str
Value:
'error'                                                                

labelClass

Type:
str
Value:
'label'                                                                

Generated by Epydoc 2.1 on Mon Jan 1 16:34:19 2007 http://epydoc.sf.net